版本:
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"/>