在springmvc中使用hibernate-validate
http://www.cnblogs.com/leechenxiang/p/5490930.html
在springmvc.xml中加入
<!-- 国际化配置 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:resource/ValidationMessages</value> </list> </property> <property name="useCodeAsDefaultMessage" value="true" /> </bean> <!-- 注册验证器 --> <mvc:annotation-driven validator="validator" /> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator" /> <!-- 这里配置将使用上面国际化配置的messageSource --> <property name="validationMessageSource" ref="messageSource" /> </bean>
依赖包:
validation-api.jar hibernate-validator.jar
<dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.2.Final</version> </dependency>