Re: Mixing EL and Java expressions
Luca Passani <[email protected]>
| Newsgroups | gmane.comp.jakarta.taglibs.user |
|---|---|
| Message-ID | <[email protected]> |
Since Martin Cooper is probably still sleeping in his time zone, here is
how I fixed the constants problem thanks to him once.
You need something called the unstandard tag-lib by Apache:
http://jakarta.apache.org/taglibs/sandbox/doc/unstandard-doc/index.html
once you have that, you can do something like (SomeClassOrInterface is
where your constants are defined):
<un:useConstants var="ct" className="com.mycom.app.SomeClassOrInterface" />
<c:forEach var="item" items="${list}">
<c:choose>
<c:when test="${item.type == ct.EMPLOYEE}">
bla bla relevant to employee
</c:when>
Let me know if this helps
Luca
Tarek Nabil wrote:
>Hi everyone,
>
>
>
>I'm moving from the Struts tag libraries to JSTL and I'm having a bit of
>a hard time. Things don't work as I thought they would. For example, I'm
>not able to use constants defined in classes for comparison, which is
>something I used to do easily with Struts tags. Here's an example; I
>want to do something if the display parameter equals the constant
>DISPLAY_BY_SERVICE defined in some class and do something else if it has
>the value DISPLAY_BY_BU. First, I tried this
>
>
>
> <c:choose>
>
> <c:when test="${param.display == <%=
>MainPageAction.DISPLAY_BY_SERVICE %>}">
>
> By Service
>
> </c:when>
>
> <c:when test="${param.display == <%= MainPageAction.DISPLAY_BY_BU
>%>}">
>
> By BU
>
> </c:when>
>
> </c:choose>
>
>
>
>That didn't even compile, so I learned my first lesson: I can not use
>Java expression inside EL expressions. So, I tried this
>
>
>
> <c:set var="displayParam" value="${param.display}"/>
>
> <c:choose>
>
> <c:when test="<%=
>MainPageAction.DISPLAY_BY_SERVICE.equals(displayParam) %>">
>
> <%= MainPageAction.DISPLAY_BY_SERVICE %>
>
> </c:when>
>
> <c:when test="<%=
>MainPageAction.DISPLAY_BY_BU.equals(displayParam) %>">
>
> <%= MainPageAction.DISPLAY_BY_BU %>
>
> </c:when>
>
> </c:choose>
>
>
>
>That didn't compile either, so now I know that unlike <bean:define>,
><c:set> doesn't define a scripting variable, but only a page scope
>variable. So, I tried this
>
>
>
> <c:choose>
>
> <c:when test="${param.display} == <%=
>MainPageAction.DISPLAY_BY_SERVICE %>">
>
> By Service
>
> </c:when>
>
> <c:when test="${param.display} == <%= MainPageAction.DISPLAY_BY_BU
>%>">
>
> By BU
>
> </c:when>
>
> <c:otherwise>Otherwise</c:otherwise>
>
> </c:choose>
>
>
>
>Now, that compiled. But it didn't work!!! So, now I know that the value
>of test should either be the String true or an expression that evaluates
>to true, so mixing expressions won't work this way.
>
>
>
>Now, this is what worked, and it's not elegant at all.
>
>
>
> <c:set var="DISPLAY_BY_SERVICE" value="<%=
>MainPageAction.DISPLAY_BY_SERVICE %>"/>
>
> <c:set var="DISPLAY_BY_BU" value="<%= MainPageAction.DISPLAY_BY_BU
>%>"/>
>
> <c:choose>
>
> <c:when test="${param.display == DISPLAY_BY_SERVICE}">
>
> By Service
>
> </c:when>
>
> <c:when test="${param.display == DISPLAY_BY_BU}">
>
> By BU
>
> </c:when>
>
> <c:otherwise>Otherwise</c:otherwise>
>
> </c:choose>
>
>
>
>What if I had 5 constants, do I have to re-define the 5 constants as
>vars using <c:set>? Either I'm missing something or JSTL is really not
>what I thought it was. I mean, maybe I should go back to the bean and
>logic libraries from Struts, they used to really do the job for me.
>
>
>
>Could someone please guide me on this issue?
>
>
>