Resources

SAS® AppDev Studio 3.0 Developer's Site

About the JSTL EL Example

This example illustrates using some tags from the JSTL Expression Language library:

The example has an initial page with a table listing items for sale at a mythical Web site:

Browsing the index page

Clicking on a Details link for one of the items displays a details page about that item:

Viewing details

How it works

See JavaServer Pages Standard Template Library for a brief introduction to the JSTL tags.

Flow chart for JSTL Expression Language tags

This is a simple example to illustrate use of the JSTL Expression Language tags. Although you might not want to use the JSTL XML tags in a production environment, the XML tags are handy for prototyping. For example, you can open and parse an XML file with just a couple of lines in a JSP, and select and output the content of a desired XML element with a single tag.

To focus exclusively on the JSTL tags and to keep things simple, this example does not use any SAS-specific content.

Items Available page operation (index.jsp and parseItemList.jsp)

The entry point into this Web application is index.jsp. It displays a table, where each row is a product item obtained from an XML data file, items.xml. The first thing of significance in index.jsp is a <jsp:include> tag for parseItemList.jsp.

parseItemList.jsp contains the JSTL tags that actually open and parse the XML data file. It only takes two lines to do this and to save the resulting tree object in an application-scoped variable, itemlist:

  <c:import url="/WEB-INF/items.xml" var="xml" />
  <x:parse xml="${xml}" var="itemlist" scope="application" />

${xml} is the Expression Language (EL) code to refer to the attribute value created by the var attribute of the first line. These two lines are enclosed by a <c:if> tag (not shown), so that the XML document will only be opened and parsed once, no matter how many user sessions there are, or if parseItemList.jsp is included in other pages.

Back in index.jsp, the iteration to extract each data item node from the tree is done with this JSTL XML tag:

  <x:forEach var="curItem" select="$itemlist/items/item">

The select attribute specified is not an EL expression (it has a $ but no curly brackets). It is an XPath expression telling which elements should comprise the collection to iterate over.

Then these co-operating tags save the value of the id attribute for the current item in the page-scoped variable curId for later use:

  <c:set var="curId">
    <x:out select="$curItem/@id"/>
  </c:set>

The data cell contents for the table row are rendered using more <x:out> tags to select appropriate sub-elements of curItem. The last cell of each row in the table will have a Details link for that item. This link has a URL that includes a parameter identifying the particular item's id attribute. The value for this is curId variable which was previously saved. The composite URL is built up using these cooperating JSTL tags:

    <c:url var="url" value="/showDetail.jsp?" >
     <c:param name="id" value="${curId}"/>
    </c:url>

Finally, the link is rendered with this tag:

    <a href="<c:out value='${url}'/>">Details</a>

Note the use of single quotes within the href attribute for the value attribute of the JSTL tag.

Item Details page operation (showDetail.jsp)

The Item Details page is normally reached when a user clicks on the Details link in one of the rows of the table in the Items Available page. When a request arrives at showDetails.jsp, the following tag extracts the value of the id parameter from the request, and uses it to select the XML item node that has a matching id attribute, saving that node in the itemSel page-scoped variable:

   <x:set var="itemSel" select="$itemlist/items/item[@id=$param:id]"/>

Once the selected item is determined, then JSTL <x:out> tags are used to render the mfg, model and name sub-element element values from the XML data. An <x:forEach> tag then iterates through all the bullet sub-elements, displaying each in the same way. Additional <x:out> tags output the weight sub-element and its unit attribute.

A <c:choose> tag provides the logic equivalent to a java switch statement, to output the line about the shipping status, depending on the value of the shipLoc sub-element.

Finally, a check is made on the stock sub-element for the item, and if the value is less than 1, a message is output. This is done by the following lines:

   <c:set var="qty">
    <x:out select="$itemSel/ship/stock"/>
   </c:set>

   <c:if test="${qty < 1}">
    <p><b>Sorry, this item is temporarily out of stock.</b>
   </c:if>

See also: