【问题标题】:Spring custom validation messages not used未使用 Spring 自定义验证消息
【发布时间】:2014-03-07 11:18:10
【问题描述】:

我使用CustomDateEditor 来解析作为表单值提交的日期时间值,并设置ValidationMessages.properties 属性文件来提供验证失败的消息文本。但是 Spring 仍然显示默认的丑陋 IllegalArgumentException 消息,而不是我提供的消息文本。我做错了什么?

【问题讨论】:

  • 我正在回答我自己的问题,as is encouraged
  • 另见stackoverflow.com/questions/13314862/…:在那个问题中,用户没有使用消息属性文件的正常名称(ValidationMessages.properties),所以在这种情况下,您必须告诉 Spring 哪些属性更清楚要使用的文件。

标签: spring validation


【解决方案1】:

虽然ValidationMessages.properties 是验证消息的正常位置,但默认情况下 Spring 不会从那里加载消息。您必须使用 ResourceBundleMessageSource 对象明确告诉 Spring 加载这些消息。也就是说,使用这样的 bean 定义:

 <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
    <property name="basenames">
       <list>
          <value>ValidationMessages</value>
       </list>
    </property>
 </bean>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2011-05-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多