一般而言,为了方便的管理系统中的各种错误信息,提示信息,我们都会把这些信息集中放到统一的文件当中。在springmvc 的validation框架中,我们可以把此类信息放置到messages.properties(当然这个文件名自己可以随便取,只要在配置中相应的指定就可以了)文件中。对于中文的信息,我们不能直接把要显示的消息放在配置文件中,例如下图
具体做法是,我们可以先把信息保存如上图的messages.properties文件中,然后通过native2ascii来得到我们的finalMessages.properties.
finalMessages.properties中的信息如下:
这样我们的中文信息就可以正确的显示出来了 :)。
Note:spring-servlet.xml 中validaiton相关的配置。
<mvc:annotation-driven validator="validator"/>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:finalMessages"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" >
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<property name="validationMessageSource" ref="messageSource"/>
</bean>