spring message tag with multiple arguments

58,265

Solution 1

The cause of the probelm is that , (comma) is the default separator. So at the end the spring message tag will get the String A,B,C,D,E,F for parameter arguments, and it will split this string into 6 different internal arguments for the message.

You must change the separator. If you use ; for example, then it will work.

<spring:message code="messageCode"
       arguments="${value1};${value2};${value3}"
       htmlEscape="false"
       argumentSeparator=";"/>

@See Spring Reference: Appendix F.6 The Message Tag

Solution 2

You could also send the different values as an array and leave no room for spring making a mistake in how to parse the string argument.

<c:set var="value1" value="A,B;X" />
<c:set var="value2" value="C,D;Y" />
<c:set var="value3" value="E,F;Z" />

<spring:message code="messageCode"
   arguments="${[value1, value2, value3]}"
   htmlEscape="false" />

This way you need no worry about your new separator somehow being used in a value at some point again.

Solution 3

I use completely different approach. My database-based message source is exposed in my config with the name i18n:

@Bean(name = {"i18n", "messageSource"})
public MessageSource messageSource() {
    return new JpaMessageSource();
}

and I also expose my beans with viewResolver.setExposeContextBeansAsAttributes(true); After that I can use ${i18n.message("messageCode", value1, value2, value3)} in my jsp-views.

Share:
58,265
George Sun
Author by

George Sun

CDN &amp; performance professional on web &amp; mobile. Certified architect &amp; programmer. Dog lover. Creator of http://www.whatsmycdn.com, a tool to find out the CDN provider(s) for a site or domain.

Updated on June 12, 2020

Comments

  • George Sun
    George Sun almost 4 years

    I am trying to get i18n message like below:

    messageCode=Test message for {0} and {1} and {2}.

    In jsp, I have this:

    <spring:message code="messageCode" 
                    arguments="${value1},${value2},${value3}" 
                    htmlEscape="false"/>
    

    The arguments:

    value1=A,B
    value2=C,D
    value3=E,F
    

    The output for what I want:

    Test message for A,B and C,D and E,F 
    

    The actual output:

    Test message for A and B and C
    

    Is there any way to overcome this? Thank you.

    George

  • shareef
    shareef over 7 years
    adding ` htmlEscape="false" argumentSeparator=";"` solved my problem