springMvc校验

112 篇文章 0 订阅
18 篇文章 0 订阅

springMvc校验

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年8月14日 17:02:03 星期五

http://fanshuyao.iteye.com/

 

 

一、首先加入jar包

1、下载hibernate-validator校验jar包文件,把jar包放到项目里(lib)

hibernate-validator-5.2.1.Final.jar

hibernate-validator-annotation-processor-5.2.1.Final.jar

hibernate-validator-cdi-5.2.1.Final.jar

(required文件夹的包

classmate-1.1.0.jar

javax.el-2.2.4.jar

javax.el-api-2.2.4.jar

jboss-logging-3.2.1.Final.jar

validation-api-1.1.0.Final.jar

 

二、在spring mvc配置文件加上校验的bean配置,如下:

<mvc:annotation-driven validator="validator" conversion-service="conversion-service" />
    
	<!-- 下面两个bean相当于mvc:annotation-driven -->
	<!-- 
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 
    -->
    
    <!-- 校验 -->
	<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="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
    <bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
        <property name="basename" value="classpath:message"/>  
        <property name="fileEncodings" value="utf-8"/>  
        <property name="cacheSeconds" value="120"/>  
    </bean>

  

1、配置校验的bean,以及配置校验的配置文件

<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>

 

2、给校验加上国际化信息配置

其中

 <property name="basename" value="classpath:message"/>

的value值配置的是国际化的配置文件的名称,放在src目录下。如message.properties

如果需要配置英文的配置文件,增加名称为message_en_US.properties的文件

 

bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
        <property name="basename" value="classpath:message"/>  
        <property name="fileEncodings" value="utf-8"/>  
        <property name="cacheSeconds" value="120"/>  
    </bean>

 

3、可以加上类型格式化的bean

 

<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />

 

 4、把校验和类型格式化交给mvc:annotation-driven管理

<mvc:annotation-driven validator="validator" conversion-service="conversion-service" />

 

 三、在Java文件中配置注解校验

@NotEmpty(message="{student.name.empty}")
	@Length(min=6,max=20,message="{student.name.length}")
	private String name;
	
	@NotNull(message="{student.age.null}")
	@Range(min=8,max=180,message="{student.age.length}")
	private int age;

 

1、@Length 校验长度,但是校验的是字符串

2、@Range 校验的是数字类型的大小

3、message对应的为配置文件中(message.properties)的键值,注意的是message是用{ }包起来的。

 

 

 

 

 

 更多信息请参考官方文档:http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/validator-usingvalidator.html

 

 

四、在property文件配置信息

1、message.properties

 student.age.null=\u5E74\u9F84\u4E0D\u80FD\u4E3A\u7A7A\u3002
student.age.length=\u5E74\u68C0\u5FC5\u987B\u5728{min}\u81F3{max}\u4E4B\u95F4
student.name.empty=\u59D3\u540D\u4E0D\u80FD\u4E3A\u7A7A\u3002
student.name.length=\u59D3\u540D\u957F\u5EA6\u5FC5\u987B\u5728{min}\u81F3{max}\u4E2A\u5B57\u7B26\u4E4B\u95F4

 

 2、message_en_US.properties

 student.age.null=age can't be null.
student.age.length=age length must between {min} and {max}
student.name.empty=name can't be empty.
student.name.length=name length must between {min} and {max}

 

 

五、显示效果



 

 

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年8月14日 17:02:03 星期五

http://fanshuyao.iteye.com/

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值