Spring验证默认消息

Spring验证默认消息

本文介绍了Spring验证默认消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在控制器中以编程方式获取解决的错误消息。 typeMismatch错误的默认验证消息不会从我的messages.properties文件中填充。我有一个表单支持对象,其中一个字段是一个整数。如果我为该字段提交一个字符串,我得到:

 无法将类型java.lang.String的属性值转换为属性establishedYear需要输入java.lang.Integer;嵌套异常是java.lang.NumberFormatException:对于输入字符串:1995a

作为默认消息ObjectError。这是我的控制器输出:

  @RequestMapping(method = RequestMethod.POST)
public @ResponseBody FormJSONResponse postForm @Valid ProfileEditCompanyForm profileEditCompanyForm,BindingResult result)throws Exception {
if(result.hasErrors()){
for(ObjectError objectError:result.getAllErrors()){
System.out.println objectError.getDefaultMessage()); //这不是我的信息,但应该是
}
}
...其他东西...
}
/ pre>

所以我添加了一个messages.properties到WEB-INF /类与一些测试消息,看看是否可以覆盖该默认消息:

  typeMismatch.profileEditCompanyForm.establishedYear = test 1 
typeMismatch.establishedYear = test 2
typeMismatch.java.lang.Integer = test 3
typeMismatch = test 4
profileEditCompanyForm.establishedYear = test 5
establishedYear = test 6

在我的app-servlet.xml文件中,我有:

 < mvc:注释驱动的转换服务=conversionServicevalidator =validator/> 

< bean id =messageSourceclass =org.springframework.context.support.ResourceBundleMessageSource>
< property name =basenamevalue =messages/>
< / bean>

< bean id =validatorclass =org.springframework.validation.beanvalidation.LocalValidatorFactoryBean>
< property name =validationMessageSourceref =messageSource/>
< / bean>

为什么不从我的messages.properties文件中收到我的任何邮件?

解决方案

在你的Spring context中尝试这个:

 < bean id =messageSource
class =org.springframework.context.support.ReloadableResourceBundleMessageSource>
< property name =basenamevalue =WEB-INF / classes / messages/>
< / bean>

然后在WEB-INF / classes文件夹内创建一个文件调用:messages.properties / p>

记录您必须提供的messages.properties的内容:

  typeMismatch.pathValueInsideYourJSPform:input =您的消息

希望这有助于您! / p>

这里是一个也


I need to get the resolved error messages programmatically in the controller. The default validation message for typeMismatch errors are not populating from my messages.properties file. I have a form backing object where a field is an Integer. If I submit a string for that field I get:

Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'establishedYear'; nested exception is java.lang.NumberFormatException: For input string: "1995a"

as the default message in the ObjectError. Here's my controller that output it:

  @RequestMapping(method = RequestMethod.POST)
  public @ResponseBody FormJSONResponse postForm(@Valid ProfileEditCompanyForm profileEditCompanyForm, BindingResult result) throws Exception {
    if (result.hasErrors()) {
      for (ObjectError objectError : result.getAllErrors()) {
        System.out.println(objectError.getDefaultMessage());  // THIS IS NOT MY MESSAGE, BUT SHOULD BE
      }
    }
    ... other stuff ...
  }

So I added a messages.properties to WEB-INF/classes with some test messages to see if I could override that default message:

typeMismatch.profileEditCompanyForm.establishedYear=test 1
typeMismatch.establishedYear=test 2
typeMismatch.java.lang.Integer=test 3
typeMismatch=test 4
profileEditCompanyForm.establishedYear=test 5
establishedYear=test 6

In my app-servlet.xml file I have:

<mvc:annotation-driven conversion-service="conversionService" validator="validator"/>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename" value="messages" />
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
  <property name="validationMessageSource" ref="messageSource"/>
</bean>

Why isn't it picking up any of my messages from my messages.properties file?

解决方案

Try this in you Spring context instead:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/classes/messages" />
</bean>

Then inside "WEB-INF/classes" folder create a file call: "messages.properties"

Take note for the content of "messages.properties" you have to provide it like this :

typeMismatch.pathValueInsideYourJSPform:input= Your Message

Hope this helps you !

here is a sample also

这篇关于Spring验证默认消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:23