9

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'/>">
Community
  • 1
  • 1
zmanc
  • 5,201
  • 12
  • 45
  • 90
  • As I don't use it, I'm not sure Spring MVC made progress in those 3 years. One way you could try to check if the `ResourceBundle` instance isn't available as some request attribute. If so, then you could make use of plain EL like so `${bundleAttributeName['lman.ilr.closeItemDetail']}`. At least, JSF works that way. – BalusC Jul 30 '13 at 14:11
  • Uh, it's just part of standard Java SE since ages and used by `` under the covers. Did you bother to explore the available request attributes if there isn't such one? – BalusC Jul 30 '13 at 14:26
  • Probable duplicate of http://stackoverflow.com/questions/5273958/ddg#5725063 – Chloe Nov 19 '18 at 01:28

4 Answers4

10

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
5

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...
Subin Chalil
  • 3,531
  • 2
  • 24
  • 38
2

I think what you wanna do is.

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

Then

<dsg:sidePanelContent closePanelText="${closeMessage}">
0

As above mentioned,

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

<dsg:sidePanelContent closePanelText="${closeMessage}">

works, since tag has the attribute "var" for above purpose. tested with spring boot app and it works.