Spring mvc not able to read messages.properties file

為{幸葍}努か 提交于 2019-12-10 12:02:27

问题


I am trying to use custom validation error messages by using properties file. I have placed messages.properties file in web content/web-inf/ folder.

    NonEmpty.batchDomain.batchName=Invalid message 2.

My applicationContext file is :

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

And my controller is :

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

       public class BatchDomain {

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

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

 }

As far as I have seen in google, I am following the correct approach. So, what can be the reason behind this issue?


回答1:


You may try to put file "messages.properties" in /src/main/resource directory.



来源:https://stackoverflow.com/questions/18533680/spring-mvc-not-able-to-read-messages-properties-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!