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?

 

Thank you.

 

********************************************DISCLAIMER********************************************
This email and any files transmitted with it are confidential and contain 
privileged or copyright 
information. If you are not the intended recipient you must not copy, 
distribute or use this email
or the information contained in it for any purpose other than to notify us of 
the receipt thereof.
If you have received this message in error, please notify the sender 
immediately, and delete this
email from your system.

Please note that e-mails are susceptible to change.The sender shall not be 
liable for the improper
or incomplete transmission of the information contained in this 
communication,nor for any delay in
its receipt or damage to your system.The sender does not guarantee that this 
material is free from
viruses or any other defects although due care has been taken to minimise the 
risk.
**************************************************************************************************

Reply via email to