使用hibernate validation 是为了校验方便
在使用的过程中遇到了这样的问题:
在spring4.1.6版本中
我是这么配置的:
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
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/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.xxz" />
<mvc:annotation-driven />
<!-- 以下 validator 在使用 mvc:annotation-driven 会 自动注册 -->
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<!-- bind your messages.properties -->
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<!-- *****不能加classpath*: 否则找不到message-->
<property name="basename" value="message" />
<!-- *****不使用默认消息
<property name="useCodeAsDefaultMessage" value="false"/>
-->
<property name="defaultEncoding" value="UTF-8"/>
</bean>
</beans>
在实体中没有给message这个值
写法如下:
public class User {
@NotEmpty
private String username;
}
在校验时竟然能够找到message.properties文件中对应的错误消息
在spring4.2.4中
我是这么配置的:
<?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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="org.xxz.web" />
<mvc:annotation-driven validator="validator" />
<!-- 数据校验 -->
<!-- 以下 validator 在使用 mvc:annotation-driven 会 自动注册 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource" />
</bean>
<!-- bind your messages.properties -->
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basenames">
<list>
<!-- *****一定不要classpath:否则找不到message.properties -->
<value>message</value>
</list>
</property>
<!-- *****不使用默认消息 但是使用默认写法,例子:类型.对象.字段 NotNull.user.id -->
<property name="useCodeAsDefaultMessage" value="false"/>
<property name="defaultEncoding" value="UTF-8" />
<property name="cacheSeconds" value="60"/>
</bean>
</beans>
对应的实体是这样的
public class User {
@NotEmpty(message="{NotEmpty.user.username}")
private String username;
}
当把第二个*****的地方改成true
去掉username上(message="{NotEmpty.user.username}")时 返回的消息不是message.properties中的消息
打*****的地方是不能改动的,怎么改动怎么错
改了好几次,都改是错的。
希望有知道的人,能够解释一下,谢谢了。