Cool JSTL tricks 7

Posted by wsargent Thu, 10 Mar 2005 19:04:00 GMT

There are all sorts of things you can do with JSTL. Beyond the immediate c:if and c:choose stuff, there’s a good deal of power built into EL itself.

Tertiary operator, for example. It helps to be concise. (Of course, it helps if I would have tested this myself – tertiary operators are JSTL 1.1 and ATG is JSTL 1.0. Oh well, there’s always scriptlets.)

<span class="<c:out value='${test ? "even" : "odd"}'/>">some text</span>

Basic arithmetic. You may not think that this is so impressive, but it helps. Before, it was test if instanceof Number and then cast down to a primitive, and all kinds of ugliness.

<dsp:tomap id="pi" param="shippingGroup.priceInfo"/>
  <dsp:getvalueof id="subtotal" param="subtotal">
  <c:set var="total" value="${subtotal + pi.rawShipping + pi.additionalShippingAmount + pi.giftWrapAmount + taxAmount}"/>
  <dsp:valueof value='<%= pageContext.getAttribute("total") %>' converter="currency"/>
</dsp:getvalueof>

And then of course there’s the dynamic map attributes. You really don’t want to look at the before version.

<dsp:getvalueof id="taxPriceInfoMap" bean="ShoppingCart.current.taxPriceInfo.shippingItemsTaxPriceInfos"

 idtype="java.util.Map"&gt;

<dsp:getvalueof id=”shippingGroupId” param=”shippingGroup.id” idtype=”java.lang.String”>

&lt;c:set var="taxAmount" value="${taxPriceInfoMap[shippingGroupId].amount}"/&gt;
&lt;dsp:valueof value='&lt;%= pageContext.getAttribute("taxAmount") %&gt;' converter="currency"/&gt;

</dsp:getvalueof> </dsp:getvalueof>

Still wish there was some kind of DynamicBeans included, but I suppose I can’t ask for too much at once.

Comments

Leave a comment

  1. Matt Payne about 10 hours later:

    Still not conviced. Just getting the size of a list is akward in jstl. Suppose you want to loop through a list of diverse objects e.g. parents and all subclasses of a given object hibernate. Accessing properties that may or may not be there is very easy with velocity(and freemarker), but jstl/jsp blows up. Also, whether you are doing jsp or velocity the usage of ognl expressions give you much more power than el.

    http://www.ognl.org/2.6.7/Documentation/html/LanguageGuide/index.html
    httlp://www.ognl.org/2.6.7/Documentation/html/LanguageGuide/apa.html
    http://www.ognl.org/2.6.7/Documentation/html/LanguageGuide/selection.html

  2. Will Sargent about 11 hours later:

    Getting the size of a list I can do with dsp:test. And looping through diverse objects is something that ATG has done for at least five years or so – in the ShoppingCart example above, it goes through four different kinds of objects and dynamically queries their properties based on the class type.

  3. Kris Schneider about 21 hours later:

    What the hey, I’ll give it one more shot (preview would be nice):

    If you’re using the EL’s conditional operator, then you’re using JSP 2.0. So, you can just do:

    <span class="${test ? "even" : "odd"}">some text</span>

    In other words, no JSTL involved. In addition, JSP 2.0 makes the EL “natively” available for request-time tag attribute values. That means you can use the EL instead of scripting expressions (<%= %>) . So, your <dsp:valueOf> tag would look like:

    <dsp:valueof value="${taxAmount}" converter="currency"/>

    or:

    <dsp:valueof value="${pageScope.taxAmount}" converter="currency"/>

    Not sure about the diverse objects blowup, but as for getting the size of a list, JSTL 1.1 provides a length function:


    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>



    ${fn:length(theList)}


  4. Will Sargent 3 days later:

    Kris, thanks for the update. You can actually do the <dsp:valueof value=”${taxAmount}”/> using the dspel taglib, but we’re not doing that for this project…

  5. Tim O'Brien 9 months later:

    Icky. Hey, what are doing adding up numbers in an EL expression for a invoice total? Doesn’t ATG Dynamo with it’s expensive commerce and personalization stack provide that for you?

    The next question is why would you be accessing pageContext in a JSP Expression when you could just access it in an EL Expression….

    Oh….I get it, ATG Dynamo isn’t JSP 2.0 yet?

    Ugh. Glad I’m no longer struggling with that heavy beast of an app server.

  6. Will Sargent 10 months later:

    I’m using DSP rather than the DSPEL tag library, so the EL expressions weren’t available. Another project, maybe.

    The customer in this case wanted a rough custom calculation on one page that didn’t include the true cost of shipping. The order runs through all the payment processors as usual at checkout.

  7. Anonymous over 4 years later:

    xvdvs

Comments