com.springsource.javax.validation-1.0.0.GA.jar 规范(只是定义)
hibernate-validator-4.1.0.Final.jar 厂商实现
log4j.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
2.校验功能实现 在实体类上添加校验注解
1 Person.java
2 @Size(min=3,max=10,message="用户名必须大于3个字符,小于10个字符")
3 private String name;
4 @NotNull(message="年龄不能为空")
5 private Integer age;
6 personUpdate.jsp
7
8 name: <sf:input path="name"/>
9 <font color="red"><sf:errors path="name"/></font>
10
11 PersonController.java
12 public String saveOrUpdate(@Valid Person p, BindingResult br,
13 @RequestParam("photo") MultipartFile file,
14 HttpServletRequest request, HttpServletResponse repsonse){
15 if(br.hasErrors()){
16 return “person/jPersonUpdate”; //有错误返回页面显示错误
17 }
校验错误信息回显
<sf:errors path=“*"/>
显示所有错误
注意:
form属性 modelAttribute="person“
否则将不能显示错误信息
原文:http://www.cnblogs.com/s1297-lgy/p/7456114.html