一般而言,爲了方便的管理系統中的各種錯誤信息,提示信息,我們都會把這些信息集中放到統一的文件當中。在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>