This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Processing dynamic variable in JSTL

 
mahesh rao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have problem getting the result of a JSTL variable dynamically. Here is the scenario.
I have defined messages for some status values as follows in the page.

<c:set var="messagestatus1a" value="Less than 30 days with ME" /> <c:set var="messagestatus1b" value="30 days or more with ME" /> <c:set var="messagestatus2a" value="Less than 30 days with JPM"/> <c:set var="messagestatus2b" value="30 days or more with JPM" />

I get a status value from the database (such as status1a, status1b, status2a, status2b) and I am concatenating with 'message' so that I can frame the appropriate variable name.

<c:forEach var="suggestionsLoop" items="${suggestionQuery.rows}">
<c:set var="msgDesc" value="message${suggestionsLoop.status}" />
<!-- Now, 'msgDesc' variable has a value like 'messagestatus1a' or 'messagestatus1b' etc.,
I would like to reprocess the value from msgDesc to diaplay the appropriate message.
If I try ${${msgDesc}} obviously I am getting error. Any ideas how to accomplish this ?
-->
</c:forEach>

Thanks,
vmrao
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mahesh rao:
I have defined messages for some status values as follows in the page.


Rather than hard-coding these on the page, you might want to consider putting them in a Map established in app scope at startup.

${${msgDesc}} obviously I am getting error


Do you have a copy of the JSP Spec which has a section on the syntax of the EL? If not, you should!

Try ${pageScope[msgDesc]}
 
mahesh rao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that worked.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mahesh rao:
Thanks, that worked.


That's cool! But do you understand why that works?
 
mahesh rao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes (pageScope is default scope). Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic