Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML forms with java Play Framework 2

am using a form with just one input text and one submit button. I want to forward the text from the input text field to a controller at the backend. perhaps if you take a look at the code snippet it gives a better picture of what I am trying to do.

this is from the index.html page

@helper.form(action=routes.Application.index()){       
<input type='text' name='myname' />
<input type='submit' name='mysubmit' value='Create Class' />    
}

below is a code snippet from the controller

public class Application extends Controller {

public static Result index() {
return ok(index.render(null)); }
}

the code displays the form as expected but I want to pass the string entered into the input text field to the controller method and then print the text. Like what is presented below.

System.out.println(variable);

where variable is the the test entered into the text field. Any suggestions will be welcomed.

like image 629
faisal abdulai Avatar asked Dec 15 '22 20:12

faisal abdulai


1 Answers

Use DynamicForm for that:

public static Result index() {
    DynamicForm bindedForm = form().bindFromRequest();
    System.out.println(bindedForm.get("myname"));
    // or...
    Logger.info(bindedForm.get("myname")); 
        // Play's Logger is nicer than System.out.println();
    return ok(index.render(null)); }
}
like image 145
biesior Avatar answered Jan 01 '23 17:01

biesior