如果是POST
表单,则无需在以下声明中声明参数route
:
POST /login/submit controllers.Users.loginSubmit()
模板:
<!-- syntax: @routes.ControllerName.methodName() -->
<form method="post" action="@routes.Users.loginSubmit()">
<input type="text" name="username" /><br/>
<input type="password" name="password" /><br/>
<input type="submit" value="Login" />
</form>
进口:
import play.data.DynamicForm;
import play.data.Form;
控制器:
public static Result loginSubmit(){
DynamicForm dynamicForm = Form.form().bindFromRequest();
Logger.info("Username is: " + dynamicForm.get("username"));
Logger.info("Password is: " + dynamicForm.get("password"));
return ok("ok, I recived POST data. That's all...");
}
模板表单助手
Play 的模板中也有表单模板助手可用于创建表单,因此可以执行以下操作:
@helper.form(action = routes.User.loginSubmit()) {
<input type="text" name="username" /><br/>
<input type="password" name="password" /><br/>
<input type="submit" value="Login" />
}
它们在处理大型和/或pre-filled
表单时特别有用