SpringMVC验证分组

本文介绍如何在SpringMVC中实现自定义分组验证,通过创建标记接口和使用对象属性验证注解,实现对不同场景下用户输入的灵活验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值