Spring message in JSTL Tag

30,582

Solution 1

The spring message tag, just as fmt:message, has a var attribute that can be used to store the message instead of displaying it.

It always helps to read the documentation.

Also, your wrong message probably comes from forgettin to declare the spring taglib at the top of your JSP.

Solution 2

In case for reference,

<c:choose>
  <c:when test="${serviceVO.id eq 0}"> 
     <spring:message code="label.service.createservice" var="buttonName"/> 
  </c:when> 
  <c:otherwise>
    <spring:message code="label.updateservice" var="buttonName"/> 
  </c:otherwise>
</c:choose>

<c:out value="${buttonName}"> //Prints the desired value...

Solution 3

I think what you wanna do is.

<spring:message code='lman.ilr.closeItemDetail' var="closeMessage"/>

Then

<dsg:sidePanelContent closePanelText="${closeMessage}">
Share:
30,582
zmanc
Author by

zmanc

Updated on June 08, 2020

Comments

  • zmanc
    zmanc almost 4 years

    According to this post from 3 years ago the only way to display a spring message in a jstl tag is to wrap it in a <c:set var="someVar"> which does "work" but it seems very far from ideal.

    Fast forward 3 years, is this still the only way to handle this?

    Here is my code

    Works, but not "ideal"

    <c:set var="closeMessage">
        <spring:message code='lman.ilr.closeItemDetail'/>
    </c:set>
    <dsg:sidePanelContent closePanelText="${closeMessage}">
    

    Doesn't work, returns a string of <spring:message code='lman.ilr.closeItemDetail'/>

    <dsg:sidePanelContent closePanelText="<spring:message code='lman.ilr.closeItemDetail'/>">
    
  • zmanc
    zmanc almost 11 years
    JB, The wrong message and the working / non ideal are on the same JSP, and spring is declared.
  • Michael Z
    Michael Z over 10 years
    @JBNizet thank trick with <spring:message var="label" code='my.message.text'/> works great for me!
  • Matt Ke
    Matt Ke almost 4 years
    This should be a comment.