ATG Currency Converter Not Working With JSTL c:set

Currency conversion confusion on Flickr!

This JSP code was not outputting the properly formatted amount.

<dspel:valueof value="${dollars}" converter="currency" />

I tried forcing it by specifying the conversion parameters.

<dspel:valueof value="${dollars}" converter="currency" locale="en-CA" format="#.00" currency="$" />

I then debugged atg.droplet.CurrencyTagConverter using Eclipse and found this code being executed.

public String convertObjectToString(DynamoHttpServletRequest pRequest,
                                 Object pValue, Properties pAttributes)
  throws TagConversionException
  if ((pValue == null) || (pValue instanceof String))
    return (String)pValue;

It turns out pValue was a String which is why it was never being formatted.  But why is it a String?

Well the code that sets dollars looks like this.

<dspel:tomap var="paymentGroup" param="commerceItem.paymentGroups[1]" />
<c:set var="dollars" value="0" />
<c:if test="${not empty paymentGroup}">
  <c:set var="dollars" value="${paymentGroup.amount}" />

Apparently c:set converts the Double to a String.

To work around this problem I did this.

$<dspel:valueof number="0.00" value="${dollars}" />

For further reading please see Tag Converters in the ATG Page Developer’s Guide.

ATG ForEach Output with Separators Between Items

Pez Collection

Sometimes you want to list each item in the collection with a separator in between like a comma or slash.  However the trick is to have the separator only in between items.

In ATG DSP you can use the ForEach droplet to iterate through each item in a collection.  However to put a separator between each item is not straight-forward with this droplet.  To do this I used JSTL to test if we are at the end of the collection.  If we are then I don’t add the separator, otherwise I do.

Here is an example.

<dspel:droplet name="/atg/dynamo/droplet/ForEach">
 <dspel:param name="array" param="dvds"/>
 <dspel:setvalue param="dvd" paramvalue="element" />
 <dspel:oparam name="outputStart">
  <dspel:getvalueof var="size" param="size" />
 <dspel:oparam name="output">
  <dspel:valueof param="dvd.title" valueishtml="true"/>
  <dspel:getvalueof var="count" param="count" />
  <c:if test="${size != count}"> , </c:if>

And here is the output for this example.

The Incredibles, Ratatouille, Cars

Do you have a better or more elegant solution?  Please feel free to let me know in the comments?

Get JSTL Vars from PageContext

JSTL sets its vars in the pageContext.  For example:

pageContext.setAttribute("foo", bar);

Therefore to get a JSTL variable use the pageContext within a tag or a JSP page.  For example:

// get item from pageContext and put in request
atg.servlet.DynamoHttpServletRequest drequest = atg.servlet.ServletUtil.getDynamoRequest(request);
drequest.setParameter("foo", pageContext.getAttribute("foo"));

Note that getAttribute assumes the variable is in the page scope.  If you want to get a variable from for example the request scope you would do this.

pageContext.getAttribute("foo", javax.servlet.PageContext.REQUEST_SCOPE);

For further reading please see PageContext.

Unexplainable JSP Compilation Problem

I was getting a JSP compilation problem that I could not solve.

org.apache.jasper.JasperException: Unable to compile class for JSP
        at org.apache.jasper.servlet.JspServletWrapper.handleJspException(
        at org.apache.jasper.servlet.JspServletWrapper.service(
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(
        at org.apache.jasper.servlet.JspServlet.service(

org.apache.jasper.JasperException: Unable to compile class for JSP
        at org.apache.jasper.JspCompilationContext.compile(
        at org.apache.jasper.servlet.JspServletWrapper.service(

java.lang.NumberFormatException: For input string: "${status.index}"
        at java.lang.NumberFormatException.forInputString(
        at java.lang.Integer.parseInt(
        at java.lang.Integer.valueOf(
        at org.apache.jasper.compiler.JspUtil.coerceToInt(
        at org.apache.jasper.compiler.Generator$GenerateVisitor.convertString(

The major problem was this was happening on the server but not locally.  The compilation problem was occurring for this line.

<uportal:module definition="${module}" moduleIndex="${status.index}"/>

The moduleIndex value is “${status.index}”. Locally when the JSP compiler encounters this it knows to call my setter for moduleIndex that takes a String parameter. But on the server the JSP compiler seemed to insist on using the setter for moduleIndex that takes an integer parameter, hence the compilation problem.

I finally posted on the Sun Forums and with the help of evnafets I came up with two solutions.

  1. The correct solution is to upgrade my application to use JSTL 1.1.  Then the JSTL expressions such as “${status.index}” will be evaluated by the container.  Then I don’t have to have a setter that takes a String parameter, the JSTL expression, and evaluate it myself.  I can just use a setter that takes an integer parameter.
  2. The quick, hack solution which is to get rid of the setter that takes an integer parameter.  Wherever the application passes in an integer parameter, I change it to pass in a String representation of that integer.

Upgrading to JSTL 1.1

Based on this excellent post I came up with the following instructions for upgrading from JSTL 1.1.

  1. Update URI in JSP pages to use JSTL 1.1.
    <%@ taglib prefix="c" uri="" %>
  2. Install in WEB-INF/lib the two JSTL 1.1 jars, standard.jar and jstl.jar.  You can get these from The Jakarta Site – Standard 1.1 Taglib Downloads.
  3. Update the start of web.xml to look like this.
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns=""

Test if empty in JSTL

Singur suflet pustiu on Flickr

(Photo: Singur suflet pustiu by dani81_const)

This article, Expression Language Overview” href=”″>InformIT: The JSTL Expression Language > Expression Language Overview, informed me on how to test if something is empty or not.


<c:if test="${empty foo}">...</c:if>

Not empty?

<c:if test="${not empty foo}">...</c:if>

JSTL for current URI

I was wondering how do get the current URI using JSTL and found this forum thread, JSTL EL for current page URL?.

If you want the actual URI of the JSP that is being rendered:

<c:out value="${pageContext.request.requestURI}"/>

If you want the original URI, which is probably the case, then:

<c:out value="${requestScope['javax.servlet.forward.request_uri']}"/>

In DSP if you want the original URI you can use the OriginatingRequest bean which implements the HttpServletRequest.

<dsp:valueof bean="/OriginatingRequest.requestURI" />

Size of collection in a JSP/DSP page

leaf pile on Flickr

(Photo: leaf pile by oeimah)

Sometimes in a JSP/DSP page you will want to get the size of a collection and unless you are within a Range, ForEach or similar droplet you won’t have access to this value.

Struts has a nice solution using the bean:size tag.  JSTL 1.1 has a nice solution using the fn:length function.

Here is an example of how to use Struts, DSPEL and JSTL to get the size of a collection.

  <dspel:getvalueof param="book.pages" var="pages"
  <bean:size id="numPages" name="pages"/>
  Number of Pages: <c:out value="${numPages}"/>
  Number of Pages: <dspel:valueof value="${numPages}"/>

Here is an example of how to use JSTL 1.1 and DSPEL to get the size of a collection.


  <dspel:getvalueof param="book.pages" var="pages"
  Number of Pages: <c:out value="${fn:length(pages)}"/>