Spring Boot 进阶之Web进阶 学习 - 表单验证

Girl.java中  限制年龄小于18岁
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
@Min(value = 18,message = "未成年少女禁止入内")
private Integer age;

public Girl() {
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getCupSize() {
return cupSize;
}

public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

在GirlController.java中
   /*
* 添加一个女生
* @date 2018/4/20 下午5:44
* @param [cupSize, age]
* @return com.example.girl.Girl
*/
@PostMapping(value = "/add")
public Girl girlAdd(@Valid Girl girl, BindingResult bindingResult){
if(bindingResult.hasErrors()){
System.out.print(bindingResult.getFieldError().getDefaultMessage());
return null;
}
// Girl girl = new Girl();
girl.setCupSize(girl.getCupSize());
girl.setAge(girl.getAge());

return girlReposistory.save(girl);
}
posted @ 2018-04-25 13:52  zhcnblog  阅读(181)  评论(0编辑  收藏  举报