Announcement Announcement Module
Collapse
No announcement yet.
Arguments in MessageSource are not resolved Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Arguments in MessageSource are not resolved

    I think this is trivial, but still I could not find a solution.

    We are using this messageSource:

    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basename" value="messages" />
    </bean>

    When I try to use a message with arguments, it does not resolve the arguments. Instead we see the '{0}' for example in the output.

    Is there a switch we forgot?


    Integer replacements[] = {count, columnCount};
    errors.reject("storeLocatorWrongColumnCount", replacements, "Default Message");
    return false;

    storeLocatorWrongColumnCount=Row {0,integer} of the specified CSV file has a column count of {1,integer}. Correct rows must have a column count between 6 and 8 columns.

    Any ideas?

  • #2
    Use the following definition instead of the mentioned:
    Code:
    field=storeLocatorWrongColumnCount=Row {0,number,integer} of the specified CSV file has a column count of {1,number,integer}. Correct rows must have a column count between 6 and 8 columns.
    Feel free to find more information about formatting rules at the MessageFormat documentation.

    Comment


    • #3
      messageFornat argunment replacement does not work

      Sorry, now it prints this:

      Row {0,number,integer} of the specified CSV file has a column count of {1,number,integer}. Correct rows must have a column count between 6 and 8 columns.

      :-)

      I used

      storeLocatorWrongColumnCount=Row {0,number,integer} of the specified CSV file has a column count of {1,number,integer}. Correct rows must have a column count between 6 and 8 columns.

      so I dropped the field= which I guess was just a typo, right?

      Comment


      • #4
        Following example works fine for me:

        spring-config.xml

        HTML 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"
               xmlns:aop="http://www.springframework.org/schema/aop"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:util="http://www.springframework.org/schema/util"
               xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
        
            <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
                <property name="basename" value="messages"/>
            </bean>
        
        </beans>
        messages.properties

        Code:
        field=Row {0,number,integer} of the specified CSV file has a column count of {1,number,integer}. Correct rows must have a column count between 6 and 8 columns.
        SpringStart.java

        Code:
        package com.spring;
        
        import java.util.Locale;
        
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public class SpringStart {
        	public static void main(String[] args) throws Exception {
        		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        		System.out.println(context.getMessage("field", new Object[] {1, 2}, Locale.US));
        	}
        }
        Output:

        Code:
        Row 1 of the specified CSV file has a column count of 2. Correct rows must have a column count between 6 and 8 columns.

        Comment


        • #5
          Error in command....

          Hi,

          first of all thanx a lot. I am getting closer to solve it.

          I am using the ReloadableResourceBundle... now and that at least gives me the default message that I use in my error. I am trying to use the message in a Spring MVC controller like this:

          errors.reject("storeLocatorWrongColumnCount", new Object[] {count, columnCount}, "Default Message");

          count and columnCount are both ints.

          IN the view I see now the errror 'Default Message', at the same time spring gives me this error in the logs:

          Error in object 'command': codes [storeLocatorWrongColumnCount.command,storeLocatorW rongColumnCount]; arguments [1,9]; default message [Default Message]} and static attributes {}

          I am sorry, but this error message does not really help me, does it make sense for anyone? What is wrong with my code here.... Any hints?

          Thanx
          Sven

          Comment


          • #6
            Try to check the configuration at first. Perform the following call directly instead of calling errors.reject():
            Code:
            LOGGER.info(context.getMessage("field", new Object[] {1, 2}, Locale.US))
            E.g. when you start handling the request perform the call above and check the log. you see expected message if config is ok then.

            Comment


            • #7
              still does not work...

              Hi,

              when we switched to that reloadable version of the message resources, it picked up teh default message in my errors.reject() calls, but when I hit the context.getMessage() it actually threw an exception that it could not find the resource bundle with the defailt _en_US locale.

              It should pick up messages.properties, even if my local is en_US, right, as it falls back to the default. Even creating that file and checking it is really copied into the classpath did not change that.

              So we again use the
              org.springframework.context.support.ResourceBundle MessageSource
              with Spring 2.5.2 now.

              The context.getMessage() call works with that. We see the correct output with the replacements taken place.

              But not so in the errors.reject() call. Nothing is replaced here.

              :-( This is very frustrating, has anyone another idea? Could this be even a bug in Spring? I am assuming though, that this is pretty basic stuff that is not broken and just something in our config is wrong. But other then

              <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
              <property name="basename" value="messages" />
              </bean>

              in our spring config xml we have nothing configured special.

              Anyone?

              Comment


              • #8
                Solution

                Ok, we found it. After step-debugging deep into the spring code.

                Of course the spring code is all good.

                We were using the spring:message tag in a wrong way. This is the right way:

                <c:forEach var="errorObject" items="${errors.allErrors}">
                <li><spring:message code="${errorObject.code}" arguments="${errorObject.arguments}" text="${errorObject.defaultMessage}"/></li>
                </c:forEach>

                Thanx all for helping!

                Cheers
                Sven

                Comment

                Working...
                X