Announcement Announcement Module
Collapse
No announcement yet.
Unable to interpolate messages from validator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to interpolate messages from validator

    Hi,

    I have a validator that builds a constraint template with the usual code surrounded by {}. It seems quite typical.

    Code:
            if (!value.matches(regex)) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("{invalid.username.format}")
                        .addConstraintViolation();
                return false;
            }
    I have setup the servlet context with the usual.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- Resolve logical view names to .jsp resources in the /WEB-INF/views directory -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
              id="messageSource">
            <property name="basenames" value="/WEB-INF/i18n/messages, /WEB-INF/i18n/application"/>
            <property name="fallbackToSystemLocale" value="true"/>
            <!--todo: change cache level in production for performance, put in properties file-->
            <property name="cacheSeconds" value="0"/>
        </bean>
    
        <bean id="bundleLocator"
              class="org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator">
            <constructor-arg ref="messageSource"/>
        </bean>
    
        <bean id="messageInterpolator"
              class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator">
            <constructor-arg ref="bundleLocator"/>
        </bean>
    
        <bean id="validator"
              class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
            <property name="messageInterpolator" ref="messageInterpolator"/>
            <property name="validationMessageSource" ref="messageSource"/>
        </bean>
    
    </beans>
    The code exists in the messages.properties file in the i18n directory.

    But the code {invalid.username.format} is never interpolated.

    I should add that I am not using jsp (in this specific case). It is a restful service and I'd like to send back the interpolated message when there is a validation error. I'm using the @Valid and BindingResult arguments in the controller method as per the usual idiom.

    I have done this before on other projects. If anyone knows what I might be missing please let me know. I appreciate it very much.

    I can post whatever other information is needed. Please let me know.

    For completeness here also is my web.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    classpath:spring/application-context.xml,
                    /WEB-INF/spring/appServlet/servlet-context.xml
                </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <filter>
            <filter-name>httpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>httpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    </web-app>
    Thanks,
    Matt
    Last edited by matt.friedman; Feb 9th, 2013, 09:04 AM.

  • #2
    Is it because I haven't put the context loader listener into my web.xml?

    I suspect so but I would like to get some clarity on this if someone will indulge me.

    Many thanks,
    Matt

    Comment


    • #3
      Just fyi, I fixed this by putting a ValidationMessages.properties file in the root of the classpath.

      I don't think the hibernate validator I'm working with will work with the messageSource. I did run across a bug reference for that but I've not been able to find it again.

      Thanks,
      Matt

      Comment

      Working...
      X