zmanc
zmanc

Reputation: 5409

Spring message in JSTL Tag

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'/>">

Upvotes: 9

Views: 34865

Answers (4)

Hasanka Sapumal
Hasanka Sapumal

Reputation: 61

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.

Upvotes: 0

Franz Joseph
Franz Joseph

Reputation: 41

I think what you wanna do is.

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

Then

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

Upvotes: 2

Subin Chalil
Subin Chalil

Reputation: 3660

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...

Upvotes: 5

JB Nizet
JB Nizet

Reputation: 691715

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.

Upvotes: 10

Related Questions