【问题标题】:Spring validations default messagesSpring验证默认消息
【发布时间】:2012-10-30 04:10:04
【问题描述】:

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

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"

作为 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());  // THIS IS NOT MY MESSAGE, BUT SHOULD BE
      }
    }
    ... other stuff ...
  }

所以我向 WEB-INF/classes 添加了一个带有一些测试消息的 messages.properties,以查看是否可以覆盖该默认消息:

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: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>

为什么它没有从我的 messages.properties 文件中提取任何消息?

【问题讨论】:

  • messages.properties 不应该位于/src/main/java 文件夹中吗?

标签: spring validation spring-mvc error-handling messages


【解决方案1】:

在你的 Spring 上下文中试试这个:

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

然后在“WEB-INF/classes”文件夹中创建一个文件调用:“messages.properties”

注意“messages.properties”的内容,您必须像这样提供它:

typeMismatch.pathValueInsideYourJSPform:input= Your Message 

希望对你有帮助!

这里还有一个sample

【讨论】:

    【解决方案2】:

    尝试指定完整路径并尝试

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

    【讨论】:

    • 不起作用。我已经尝试了该值属性中我能想到的所有内容,现在我的 messages.properties 文件到处乱扔,希望它能找到一些东西。
    【解决方案3】:

    显然我必须通过 Spring MessageSource 运行 FieldError 对象。我希望这是自动完成的。我在这里找到了答案:

    How to get error text in controller from BindingResult

    【讨论】:

    • 如果您使用的是 Arthur 给出的更新答案,那么这意味着他正在检查自定义验证器中的验证。所以您不需要使用验证器的 bean id...如果您使用答案在更新答案之上,那么您需要在 servlet.xml 中使用您的 bean id 验证器..它正确吗?..我在关注吗?..
    • 我的问题是为什么你使用 org.springframework.validation.beanvalidation.LocalValidatorFactoryBean...所以它自己做验证...?
    猜你喜欢
    • 2020-01-17
    • 2014-04-04
    • 1970-01-01
    • 2011-02-18
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多