Spring mvc无法读取messages.properties文件

时间:2013-08-30 13:06:49

标签: spring-mvc spring-validator

我正在尝试使用属性文件来使用自定义验证错误消息。我已将messages.properties文件放在web内容/ web-inf /文件夹中。

    NonEmpty.batchDomain.batchName=Invalid message 2.

我的applicationContext文件是:

      <context:component-scan base-package="com.coaching.controller" />

<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views 
    directory -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">



    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<mvc:default-servlet-handler />
<mvc:resources mapping="/resources/**" location="/resources/" />

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

    <property name="basename" value="/WEB-INF/messages" />

</bean>

我的控制器是:

    @RequestMapping(method = RequestMethod.POST)
public ModelAndView addBatch(
        @Valid @ModelAttribute("batchDomain") BatchDomain batchDomain,
        BindingResult result, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    try {

        if (result.hasErrors()) {
            ModelAndView modelAndView = new ModelAndView("newBatch");
            return modelAndView;
        }
    }

BatchDomain是:

       public class BatchDomain {

@NotNull
@NotEmpty
@Size(min = 1, max = 100)
private String batchName;

@Min(1)
@Max(1000)
private int strength;

 }

据我在google中看到,我正在遵循正确的方法。那么,这个问题背后的原因是什么呢?

1 个答案:

答案 0 :(得分:0)

您可以尝试将文件“messages.properties”放在/ src / main / resource目录中。