我正在尝试在Play 2.1 RC2中提交用户表单。
这里是users.scala.html,它显示用户表单:
@(users: List[User], userForm: Form[User], isReadOnly: Boolean)
@import helper._
@import security._
@helper.form(action = routes.Users.updateOrCreate(), 'id->"User_Form"){
@*Variable inputs*@
@if(users == null) {
@*name field for Users.create, update, and read*@
@input(userForm("username"), '_showConstraints-> true, '_label->"User: ", 'class->"selector", '_id->"username", 'name->"username", 'id->"username"){ (id, name, value, args) => <input type="text" @if(isReadOnly){readonly}>}
} else{@if(users!=null && userForm("id")==null){
@*name field for Users.fillFrom and Users.all*@
@if(users.size < 15){
<div id = User_username-select>
<label for="username">User: </label>
<select name="username" id="username" form=@userForm</select>
</div>
}else{
@inputText(userForm("username"), '_id->"username", 'class->"selector", '_showConstraints->false, '_label->"User: ") }
}else{@*this has been reached if no name field of any sort displays*@}}
@*non-variable fields*@
@input(userForm("password"), '_id->"User_password", '_showConstraints-> true, '_label->"Password: "){ (id, name, value, args) => <input type="password" @if(isReadOnly){readonly}>}
@input(userForm("name"), '_id->"User_name", '_showConstraints-> true, '_label->"Name: ", 'readonly->isReadOnly){ (id, name, value, args) => <input type="text" @if(isReadOnly){readonly}>}
@input(userForm("role"), '_showConstraints-> true){ (id, name, value, args) => <input type="text" @if(isReadOnly){readonly}>}
<input type="submit" value="submit">
}
它所遵循的路由文件的行是POST /models/User/updateOrCreate controllers.Users.updateOrCreate()
,它引导它在Users控制器中使用此方法:
public static Result updateOrCreate(){
//check form for errors
Form<User> filledForm = userForm.bindFromRequest();
System.out.println("INSIDE UPDATEORCREATE: ");
System.out.println(request().body().asText());
System.out.println(userForm.data());
System.out.println(filledForm.data());
if(filledForm.hasErrors()){
System.out.println(filledForm.errors());
return badRequest(users.render(null, filledForm, false));
}
//saves the User and returns a string containing the id
//return redirect(routes.Users.read(User.updateOrCreate(filledForm.get())));
return ok(users.render(null, filledForm, false));
}
然而,控制器方法中的printlns分别打印“null”,“{}”和“{}”。我已经在模型中注释了所有验证,因此问题不是验证失败;由于某种原因,表单中的数据永远不会在请求中提交。