JSR-303 Spring MVC 消息国际化 配置

本文中使用的个软件版本(见最下方截图):

Spring:4.1.1

JSR 303 Validator: 1.0.0

JSR 303 Validator实现:hibernate 4.3.2

说明:为什么这里选择hibernate 4.3.2,而没有选择更高版本?

原因:Hibernate Validator 4.X 版本是完全基于JSR303;5.X版本还实现了JSR349的特性。

What’s the difference between Hibernate Validator 3, 4 and 5?

Hibernate Validator 3.x and 4.x/5.x are different codebases.

Hibernate Validator is the original validation framework from the Hibernate team and is now referred to as "Legacy Hibernate Validator". 

Hibernate Validator 4.x is the reference implementation of Bean Validation 1.0 (JSR 303),while Hibernate Validator 5.x is the reference implementation of Bean Validation 1.1 (JSR 349). Active development happens on the 5.x codebase.


扫描二维码关注公众号,回复: 4110669 查看本文章

一、使用Hibernate Validator内置的国际化消息配置

1. 引入所需要的包

2. 配置

[html] view plain copy
  1. <mvc:annotation-driven />  
[html] view plain copy
  1. <mvc:annotation-driven />  

3. 在需要验证的对象属性字段上用注解校验

[java] view plain copy
  1. <pre name="code" class="java">public class Application {  
  2.     private int appId;  
  3.     private int appServerId;  
  4.     @NotBlank  
  5.     private String appName;  
  6.     @NotBlank  
  7.     private String appUri;  
  8.     private String logFilePrefix;  
  9.     private String logFileSuffixPattern;  
[java] view plain copy
  1. <pre name="code" class="java">public class Application {  
  2.     private int appId;  
  3.     private int appServerId;  
  4.     @NotBlank  
  5.     private String appName;  
  6.     @NotBlank  
  7.     private String appUri;  
  8.     private String logFilePrefix;  
  9.     private String logFileSuffixPattern;  

4. 在Controller中编写验证逻辑

[java] view plain copy
  1. @RequestMapping(value = "/create", method = RequestMethod.POST)  
  2. public String createApp(@Valid @ModelAttribute("app") Application application, BindingResult bindingResult, Model model) {  
  3.     if (bindingResult.hasErrors()) {  
  4.         return "app/create";  
  5.     }  
  6.     this.applicationService.addApplication(application);  
  7.     return "redirect:app/list";  
  8. }  
[java] view plain copy
  1. @RequestMapping(value = "/create", method = RequestMethod.POST)  
  2. public String createApp(@Valid @ModelAttribute("app") Application application, BindingResult bindingResult, Model model) {  
  3.     if (bindingResult.hasErrors()) {  
  4.         return "app/create";  
  5.     }  
  6.     this.applicationService.addApplication(application);  
  7.     return "redirect:app/list";  
  8. }  

5. 页面错误消息配置

[html] view plain copy
  1. <body>  
  2. <sf:form action="/finder/app/create" method="post" modelAttribute="app">  
  3.     appServerId:<sf:select path="appServerId" items="${appServers}" itemValue="appServerId" itemLabel="appServerUri" /><br />  
  4.     appName:<sf:input path="appName" id="appName"/><sf:errors path="appName" /><br />  
  5.     appUri:<sf:input path="appUri" id="appUri"/><sf:errors path="appUri" /><br />  
  6.     logFilePrefix:<sf:input path="logFilePrefix" id="logFilePrefix"/><sf:errors path="logFilePrefix" /><br />  
  7.     logFileSuffixPattern:<sf:input path="logFileSuffixPattern" id="logFileSuffixPattern"/><sf:errors path="logFileSuffixPattern" /><br />  
  8.     <input type="submit" name="submit" value="submit" />  
  9. </sf:form>  
  10. </body>  
[html] view plain copy
  1. <body>  
  2. <sf:form action="/finder/app/create" method="post" modelAttribute="app">  
  3.     appServerId:<sf:select path="appServerId" items="${appServers}" itemValue="appServerId" itemLabel="appServerUri" /><br />  
  4.     appName:<sf:input path="appName" id="appName"/><sf:errors path="appName" /><br />  
  5.     appUri:<sf:input path="appUri" id="appUri"/><sf:errors path="appUri" /><br />  
  6.     logFilePrefix:<sf:input path="logFilePrefix" id="logFilePrefix"/><sf:errors path="logFilePrefix" /><br />  
  7.     logFileSuffixPattern:<sf:input path="logFileSuffixPattern" id="logFileSuffixPattern"/><sf:errors path="logFileSuffixPattern" /><br />  
  8.     <input type="submit" name="submit" value="submit" />  
  9. </sf:form>  
  10. </body>  

6. 测试验证消息


以上的方式是默认从Hibernate Validator包中的消息配置文件中读取的:



当我们需要自定义错误消息我们有两种方法:

1. 重写Hibernate Validator默认的消息配置文件,在其中自定义错误消息。

2. 自由指定错误消息basename,自定义错误消息。


二、 重写Hibernate Validator默认的消息配置文件,在其中自定义错误消息

此时我们只需要在classpath的根路径下放置一个同名的ValidationMessages属性文件,在其中自定义我们的国际化消息就可以实现。

1. 在跟路径下建同名配置文件,自定义错误消息


2. 验证对象上指定message的key

[java] view plain copy
  1. public class Application {  
  2.     private int appId;  
  3.     private int appServerId;  
  4.     @NotBlank(message="{appName.not.blank}")  
  5.     private String appName;  
  6.     @NotBlank(message="{appUri.not.blank}")  
  7.     private String appUri;  
  8.     private String logFilePrefix;  
  9.     private String logFileSuffixPattern;  
[java] view plain copy
  1. public class Application {  
  2.     private int appId;  
  3.     private int appServerId;  
  4.     @NotBlank(message="{appName.not.blank}")  
  5.     private String appName;  
  6.     @NotBlank(message="{appUri.not.blank}")  
  7.     private String appUri;  
  8.     private String logFilePrefix;  
  9.     private String logFileSuffixPattern;  


3. 验证错误消息


三、自定义国际化消息配置

重写Hibernate Validator国际化消息配置虽然可以达到我们首先自定义消息键值对的目的,但是这种方式约束了我们国际化消息文件的文件位置,这不利于项目结构管理。

我们希望能自定义国际化消息文件的路径、文件名和错误消息键值对。

1. 更改配置

[html] view plain copy
  1. <pre name="code" class="html">    <!-- i18n messages  -->  
  2.     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  
  3.         <property name="defaultEncoding" value="UTF-8" />  
  4.         <!-- you can config a single basename -->  
  5.         <!--<property name="basename" value="" />-->  
  6.         <!-- multi basename -->  
  7.         <property name="basenames">  
  8.             <list>  
  9.                 <value>org/dsw/config/i18n/ValidationMessages</value>  
  10.                 <value>org/dsw/config/i18n/BusinessMessages</value>  
  11.             </list>  
  12.         </property>  
  13.     </bean>  
  14.       
  15.     <!-- JSR 303 Validator -->  
  16.     <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
  17.         <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />  
  18.         <property name="validationMessageSource" ref="messageSource"/>   
  19.     </bean>  
  20.   
  21.     <mvc:annotation-driven validator="validator" />  
[html] view plain copy
  1. <pre name="code" class="html">    <!-- i18n messages  -->  
  2.     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  
  3.         <property name="defaultEncoding" value="UTF-8" />  
  4.         <!-- you can config a single basename -->  
  5.         <!--<property name="basename" value="" />-->  
  6.         <!-- multi basename -->  
  7.         <property name="basenames">  
  8.             <list>  
  9.                 <value>org/dsw/config/i18n/ValidationMessages</value>  
  10.                 <value>org/dsw/config/i18n/BusinessMessages</value>  
  11.             </list>  
  12.         </property>  
  13.     </bean>  
  14.       
  15.     <!-- JSR 303 Validator -->  
  16.     <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
  17.         <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />  
  18.         <property name="validationMessageSource" ref="messageSource"/>   
  19.     </bean>  
  20.   
  21.     <mvc:annotation-driven validator="validator" />  


2. 自定义国际化消息文件的路径、文件名和错误消息键值对。


3. 验证错误消息



软件版本:

猜你喜欢

转载自blog.csdn.net/H12KJGJ/article/details/80737634