This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing values using JSTL  RSS feed

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I hope this is the right category to post the question on JSTL.

I have a code something like this in JSP

<c:choose>
<c:when test="${item.type == 'book'}">
....................
</c:when>
<c:when test="${item.type == 'electronics'}">

Now instead of those hard coded values inside JSP like 'book' & 'electronics' I want to replace them by having them in a Constants.java
file.
So accordingly I want to replace 'book' with 'Constants.BOOK' and 'electronics' with 'Constants.ELECTRONICS'.

But surprisingly this does not work & the C tag library fails to recognize this.
I searched on google but somehow all of them use harc coded values for comparison.

Has someone used C tag library to compare values like the above situation

Regards
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the frustrations of JSTL is that it doesn't really handle constants very well. If you're OK with not having a 100% scriptlet-free JSP, here's one solution:

<c_rt:set var="book" value="<%= com.mycompany.Constants.BOOK%>
<c_rt:set var="electronics" value="<%= com.mycompany.Constants.ELECTRONICS%>

<c:choose><c:when test="${item.type == book}">
....

If you do want a 100% scriptless JSP, see the following article by Bear Bibeault, who is one of the moderators for the JSP forum. His solution takes a bit of work to set up, but once it's in place, you have an easy way of accessing constants in your JSTL.

Here's the link

This really belongs in the JSP forum, so I'm moving it there for any follow-up questions.
[ July 30, 2006: Message edited by: Merrill Higginson ]
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using a JSP2.0 container (eg Tomcat5) which understands EL out of the box, you can just use the standard JSTL tags rather than those "rt" ones.
The "rt" taglibrary should only be used on a JSP1.2 container (eg Tomcat4) where you want to use <%= expr %> with the JSTL tags.
For myself I reckon thats never.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!