[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2610
  • Last Modified:

JSTL <c:set> tag with <fmt> tag

Hi. I don't know much about JSTL, but someone with some knowledge was trying to help me out. Basically I'm trying to set a variable in JSTL with value equal to something referenced by a <fmt> tag. Then I try to get the value of this variable in a scriptlet through the pageContext. The code below doesn't work, however. All I'm getting is an empty string in the scriptlet. Any help would be appreciated.

      <c:set var="selectedValuePresented" value="<fmt:message key='<%= ResourceKeys.DF_SELECT %>'/>" />

      String selectedValuePresented = String.valueOf (pageContext.getAttribute("selectedValuePresented"));
  • 2
2 Solutions
You can't nest custom tags as attributes to other custom tags.
However, the c:set takes its value either from the "value" attribute, or the nested content.
You can probably do it like this:
 <c:set var="selectedValuePresented"><fmt:message key='<%= ResourceKeys.DF_SELECT %>'/></c:set>
I am presuming you have a JSP2.0 container (eg Tomcat 5), or are using the "runtime" version of JSTL.

I would question your need for scriptlet code though.  The whole point of JSTL is to minimize it in pages.

I think that <fmt> can also be used to set variable. Please do like
<fmt:message key='<%= ResourceKeys.DF_SELECT %>' var="selectedValuePresented" />

Please try that out

willi_gamboaAuthor Commented:
Thank you. Both approaches work. Regarding minimizing scriptlet code, I completely agree. However, I'm a front end developer whose focus is on DHTML. So any quick way to get the Java developers' code to work with my front end stuff will work for me.

Glad I could help


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now