在获取bindFromRequest()。get()形式的值时,它只返回null值。我得到的所有String类型都是null,并且整数为zer0。这是我的控制器和模型包的代码以及如何解决此错误:
enter code here
In controller:
public static Result getShow(){
Register register=Form.form(Register.class).bindFromRequest().get();
register.save();
System.out.println(register);
return ok("@Required annotation kicked in.."+register);
}
In Models:
package models;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="register")
public class Register {
//private static final long serialVersionUID = 1L;
private String firstname;
private String lastname;
@Id
private String displayname;
private String date;
private String email;
private String password;
private String confirm_password;
private String gender;
private int phone_no;
private String address;
private int zipcode;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getDisplayname() {
return displayname;
}
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirm_password() {
return confirm_password;
}
public void setConfirm_password(String confirm_password) {
this.confirm_password = confirm_password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getPhone_no() {
return phone_no;
}
public void setPhone__no(int phone_no) {
this.phone_no = phone_no;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
}
答案 0 :(得分:0)
如果bindFromRequest().get()
返回null
,则Form
无法验证。要对此进行调试,请记录Form.form(Register.class).bindFromRequest().errors()
,以查看Form
中的验证错误。除此之外,如果没有看到Register
类,以及您尝试绑定到它的数据,没有人可以告诉您错误。
你不应该盲目地呼叫get()
Form
并尝试保存它,因为这显然会失败。在尝试保存之前,至少检查它hasErrors()
。如果确实存在验证错误,则应将Form
传递回视图以向用户显示这些错误。
请参阅文档中的Handling Binding Failure。