0

我正在尝试通过使用属性文件来使用自定义验证错误消息。我已将 messages.properties 文件放在 web content/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;
        }
    }

批处理域是:

       public class BatchDomain {

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

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

 }

据我在谷歌看到的,我遵循正确的方法。那么,这个问题背后的原因可能是什么?

4

1 回答 1

0

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

于 2015-08-12T09:50:09.587 回答