我正在尝试在Java Play中填写我的用户表单! 2.4.3,但我总是得到“IllegalStateException:No value”
这是Controller的代码:
Form<User> uf = Form.form(User.class);
uf.fill( new User( "felix@abc.com" , "123") );
if (uf.hasErrors()){
return ok("Form Error");
}
// IllegalStateException: No value
uf.get();
User.class:
@Constraints.Required
private String email;
@Constraints.Required
private String password;
public User(){}
public User(String email, String password) {
this.email = email;
this.password = password;
}
//Getter
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
//Setter
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
我还尝试使用bind方法和HashMap填充Form,但得到相同的错误
Form<User> uf = Form.form(User.class);
Map<String,String> data = new HashMap();
data.put("email", "felix@abc.com");
data.put("password", "123");
User user = uf.bind(data);
if (uf.hasErrors()){
return ok("Form Error");
}
// IllegalStateException: No value
uf.get();
答案 0 :(得分:0)
.fill
方法返回一个新的Form
对象(see here)。所以我相信这应该工作(没有测试):
Form<User> uf = Form.form(User.class);
Form<User> newUf = uf.fill( new User( "felix@abc.com" , "123") );
if (newUf.hasErrors()){
return ok("Form Error");
}
// Should Work
newu=Uf.get();
似乎.bind
(as seen here)的故事也是一样的。