0

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?

vlad halmer
  • 1,211
  • 2
  • 16
  • 21
  • Can you try putting the properties file at the root of the classpath instead of inside `WEB-INF`? Change the declaration in the spring context as well. – Sotirios Delimanolis Aug 30 '13 at 14:41
  • I have placed the file in src folder as per http://stackoverflow.com/questions/11302531/how-to-place-a-file-on-classpath-in-eclipse and i have edit context file as but nothing happened. – vlad halmer Aug 30 '13 at 14:54
  • 1
    Does your messages.properties file work in other cases but is ignored for validation messages, or it doesn't work in any case? It's possible you are using hibernate validator which looks for ValidationMessages.properties (unless configured otherwise). – Krešimir Nesek Aug 30 '13 at 15:56
  • I am using import javax.validation.constraints and org.springframework.format.annotation.* validations. I am using messages.properties for validation errors only. – vlad halmer Aug 30 '13 at 16:32

1 Answers1

0

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

Yinghua
  • 441
  • 4
  • 2