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

源自:http://blog.csdn.net/xmzfighting/article/details/41146683


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

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.


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

1. 引入所需要的包

2. 配置

  1. <mvc:annotation-driven />  
<mvc:annotation-driven />

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

  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;  
<pre name="code" class="java">public class Application {
	private int appId;
	private int appServerId;
	@NotBlank
	private String appName;
	@NotBlank
	private String appUri;
	private String logFilePrefix;
	private String logFileSuffixPattern;

 

4. 在Controller中编写验证逻辑

  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. }  
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String createApp(@Valid @ModelAttribute("app") Application application, BindingResult bindingResult, Model model) {
		if (bindingResult.hasErrors()) {
			return "app/create";
		}
		this.applicationService.addApplication(application);
		return "redirect:app/list";
	}

5. 页面错误消息配置

  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>  
<body>
<sf:form action="/finder/app/create" method="post" modelAttribute="app">
	appServerId:<sf:select path="appServerId" items="${appServers}" itemValue="appServerId" itemLabel="appServerUri" /><br />
	appName:<sf:input path="appName" id="appName"/><sf:errors path="appName" /><br />
	appUri:<sf:input path="appUri" id="appUri"/><sf:errors path="appUri" /><br />
	logFilePrefix:<sf:input path="logFilePrefix" id="logFilePrefix"/><sf:errors path="logFilePrefix" /><br />
	logFileSuffixPattern:<sf:input path="logFileSuffixPattern" id="logFileSuffixPattern"/><sf:errors path="logFileSuffixPattern" /><br />
	<input type="submit" name="submit" value="submit" />
</sf:form>
</body>

6. 测试验证消息


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



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

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

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


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

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

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


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

  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;  
public class Application {
	private int appId;
	private int appServerId;
	@NotBlank(message="{appName.not.blank}")
	private String appName;
	@NotBlank(message="{appUri.not.blank}")
	private String appUri;
	private String logFilePrefix;
	private String logFileSuffixPattern;


3. 验证错误消息


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

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

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

1. 更改配置

  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" />  
<pre name="code" class="html">	<!-- i18n messages  -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="defaultEncoding" value="UTF-8" />
		<!-- you can config a single basename -->
		<!--<property name="basename" value="" />-->
		<!-- multi basename -->
		<property name="basenames">
			<list>
				<value>org/dsw/config/i18n/ValidationMessages</value>
				<value>org/dsw/config/i18n/BusinessMessages</value>
			</list>
		</property>
	</bean>
	
	<!-- JSR 303 Validator -->
	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
		<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
		<property name="validationMessageSource" ref="messageSource"/> 
	</bean>

	<mvc:annotation-driven validator="validator" />

 


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


3. 验证错误消息



软件版本:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值