四、标准校验框架validator和SpringMVC国际化配置的使用

本文介绍了如何在SpringMVC中结合Javax Validation API和Hibernate Validator进行数据校验,并实现了国际化配置。详细步骤包括:1)配置springMVC-servlet.xml,2)创建messages.properties及对应的国际化文件,3)定义领域对象User和自定义校验类,4)编写控制器UserController,5)展示register.jsp文件示例。通过这些步骤,可以实现对用户输入的有效性和格式的严格检查。
摘要由CSDN通过智能技术生成

版本:
Javax的validation-api-2.0.1.Final.jar用来进行数据校验(不推荐使用Hibernate-validator);
Hibernate-validator-6.0.11.Final.jar用来配合SpringMVC进行国际化配置

1、springMVC-servlet.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"     
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"     
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"     
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd   
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd   
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


       <!-- <context:annotation-config/>被component-scan包括 -->

       <!-- 启动spring MVC 注解 -->

       <!-- 设置使用注解的类所在的jar包以及sub package,controller类 -->
       <context:component-scan base-package="main.java.com.smart.*" use-default-filters="true">
            <!--
            <context:include-filter type="annotation" expression=""/>
            <context:exclude-filter type="annotation" expression=""/>
            -->
       </context:component-scan>

       <!-- 完成请求和注解POJO的映射 -->
       <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->

       <!-- 不处理静态资源文件 -->
       <!-- <mvc:default-servlet-handler/> -->

       <!-- 如果使用了注释后必须配置设置 -->
       <mvc:annotation-driven validator="validator"/>

<!-- start:国际化配置 -->
       <!-- 读取国际化资源文件 == 资源文件在包里面,路径写全包名-->
       <!-- 当国际化配置文件在WEB-INF下时使用org.springframework.context.support.ResourceBundleMessageSource -->
       <!--定义校验器数据源bean-->
       <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basenames" value="classpath:resources/i18n/messages" />
            <property name="cacheSeconds" value="3600" />
       </bean>

        <!-- 校验器 -->
       <bean id="validator"
            class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
            <!--指定校验器-->
            <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> 
            <!--引入校验器校验的数据源,注意ref和value的区别,前者引入一个object,后者引入string-->
            <property name="validationMessageSource" ref="messageSource"/>
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值