SpringMVC验证分组
- 自定义标记接口
- 对象属性验证注解选中标记接口名
- control方法的形参验证注解中选中标记接口名
对需要验证的属性,自由选择使用验证方式
步骤
自定义两个标记接口
public interface GroupInterface1 {
}
public interface GroupInterface2 {
}
对象属性验证注解选中标记接口名
public class User {
@NotBlank(message="ID 不能为空",groups={GroupInterface1.class})
private Integer id;
@NotBlank(message="账号不能为空",groups={GroupInterface1.class})
@Size(max=6,min=2,message="账号长度2-6位",groups={GroupInterface2.class})
private String name;
@NotBlank(message="地址不能为空",groups={GroupInterface1.class})
@Size(max=6,min=2,message="地址长度2-6位",groups={GroupInterface2.class})
private String address;
@Max(value=100,message="年龄最大100")
@Min(value=1,message="年龄最小1",groups={GroupInterface1.class,GroupInterface2.class})
private int age;
}
control方法的形参验证注解中选中标记接口名
@RequestMapping("/add")
public String add(@Validated(value={GroupInterface1.class}) User user,BindingResult br,Model m){
List<ObjectError> allErrors = br.getAllErrors();
if(allErrors!= null && allErrors.size() > 0){
for (ObjectError objectError : allErrors) {
System.out.println(objectError.getDefaultMessage());
}
m.addAttribute("errors", allErrors);
}
return "/user.jsp";
}
@RequestMapping("/update")
public String update(@Validated(value={GroupInterface2.class}) User user,BindingResult br,Model m){
List<ObjectError> allErrors = br.getAllErrors();
if(allErrors!= null && allErrors.size() > 0){
for (ObjectError objectError : allErrors) {
System.out.println(objectError.getDefaultMessage());
}
m.addAttribute("errors", allErrors);
}
return "/user.jsp";
}
在jsp中使用验证信息对象数据
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:if test="${errors ne null }">
<c:forEach items="${errors }" var="e">
${e.defaultMessage }<br>
</c:forEach>
</c:if>
spring配置文件
<!-- 开启扫描 -->
<context:component-scan base-package="com.bb.controller"></context:component-scan>
<!-- 开启SpringMVC注解 -->
<mvc:annotation-driven validator="validator" ></mvc:annotation-driven>
<!-- hibernate 校验 -->
<!--添加对JSR-303验证框架的支持 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<!--不设置则默认为classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource" ref="validatemessageSource"/>
</bean>
<bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validateMessage"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>
导入的jar包(有些没用到)
classmate-1.3.4.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
hibernate-validator-5.2.5.Final.jar
jboss-logging-3.1.3.GA.jar
jstl-1.1.2.jar
spring-aop-4.3.10.RELEASE.jar
spring-aspects-4.3.10.RELEASE.jar
spring-beans-4.3.10.RELEASE.jar
spring-context-4.3.10.RELEASE.jar
spring-core-4.3.10.RELEASE.jar
spring-expression-4.3.10.RELEASE.jar
spring-web-4.3.10.RELEASE.jar
spring-webmvc-4.3.10.RELEASE.jar
standard-1.1.2.jar
validation-api-1.1.0.Final.jar
总结:为属性配置验证注解时映射一个标记接口,获取对象时映射同一个标记接口,就做到了自由选择不同的方式验证属性。
项目打包 提取码:9b1b
环境:eclipse