这是处理登录POST请求的路由:
POST /login/submit controllers.Users.loginSubmit(user : String, password : String)
这是login.scala.html:
<form method="post" action="???">
<input type="text" name="username" /><br/>
<input type="password" name="password" /><br/>
<input type="submit" value="Login" />
</form>
我有两个问题:
感谢
答案 0 :(得分:25)
如果是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...");
}
还有form template helpers可用于在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
表单
答案 1 :(得分:3)
在Play Framework版本2.5.x Form.form() is deprecated中,您应该使用inject a FormFactory
您可以在这里找到示例:The method form(Class) from Form class is deprecated in Play! Framework
导入:
import play.data.DynamicForm;
import play.data.FormFactory;
注入:
@Inject FormFactory formFactory;
控制器:
public static Result loginSubmit(){
DynamicForm dynamicForm = formFactory.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...");
}