HTML forms with java Play Framework 2

am using a form containing only one input text and one submit button. I want to forward the text from the input text box to the controller on the backend. maybe if you look at the code snippet it will give a better picture of what I'm 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 snippet of code 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 in the input text box to the controller method, and then print the text. As shown below.

 System.out.println(variable); 

where the variable is the test entered in the text box. Any suggestions would be welcome.

+4
source share
1 answer

Use DynamicForm for this:

 public static Result index() { DynamicForm bindedForm = form().bindFromRequest(); System.out.println(bindedForm.get("myname")); // or... Logger.info(bindedForm.get("myname")); // Play Logger is nicer than System.out.println(); return ok(index.render(null)); } } 
+12
source

All Articles