我正在尝试在 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()
其引导到用户控制器中的此方法:
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));
}
然而,控制器方法中的 println 分别打印“null”、“{}”和“{}”。我已经注释掉了模型中的所有验证,所以问题不是验证失败;由于某种原因,表单中的数据从未在请求中提交。