Resources

SAS® AppDev Studio 3.0 Developer's Site

How to Build - JSTL EL Example

Creating the project and index.jsp page

  1. Start a new project by choosing File [arrow] New.
    1. Select a project type of Web Application Project and enter a project name of JSTL_EL. Click OK.
    2. In Step 1 of the wizard, click Next to accept the default location.
    3. Select Blank web application as the template and click Next.
    4. Deselect SAS Taglib & T-beans (which automatically deselects SAS runtime classes), and select JSP Standard Tag Library. Click Next.
    5. Leave all four EL libraries checked and click Next.
    6. Accept the default initial content type of Java Server Page and file name index.jsp by clicking Next.
    7. Keep the default of Tag Library JavaServer Page as the template and click Next.
    8. Accept the default Web application project options by clicking Next.
    9. Click Finish to construct and display the initial files for the project.

  2. At the end of index.jsp, add the following lines after the existing tag declarations:
    <html>
    <head><title>Items Available</title></head>
    <body>
    
    <h3>Items Available </h3>
    
    <%-- Parse XML document and get root in application scope attribute itemlist --%>
    <jsp:include page="parseItemList.htm" />
    
    <%-- Iterate through item list showing name, price of each item in table --%>
    <table border="2" cellpadding="5">
     <tr bgcolor="#CCCCCC"">
      <th>Manufacturer</th><th>Model</th><th>Item</th>
       <th>Description</th><th>Price</th><th>Details</th>
     </tr>
     <%-- Note that select attribute uses XPath notation , not JSTL EL notation --%>
     <x:forEach var="curItem" select="$itemlist/items/item">
      <%-- save value of id attribute as string in curID (not rendered) --%>
      <c:set var="curId">
       <x:out select="$curItem/@id"/>
      </c:set>
    
      <%-- Render each cell content in row of table --%>
      <tr>
       <td><x:out select="$curItem/mfg"></x:out></td>
       <td><x:out select="$curItem/model"></x:out></td>
       <td><x:out select="$curItem/name"></x:out></td>
       <td><x:out select="$curItem/description"></x:out></td>
       <td align="right">$<x:out select="$curItem/price"></x:out></td>
       <td>
        <%-- Add parameter to url to indicate which item is in this row--%>
        <c:url var="url" value="/showDetail.jsp?" >
         <c:param name="id" value="${curId}"/>
         </c:url>
        <a href="<c:out value='${url}'/>">Details</a>
       </td>
      </tr>
    
     </x:forEach>
    
    </table>
    </body>
    </html>
    

Creating the parseItemList.jsp included page

  1. Create a new JSP file, following these steps:

    1. Choose File [arrow] New.
    2. Select Java Server Page and name it parseItemList.jsp.
    3. Accept the defaults and click Next and then click Finish.
  2. At the end of parseItemList.jsp, add the following lines after the existing tag declarations:
     <c:if test="${applicationScope.itemlist == null}" >
      <c:import url="/WEB-INF/items.xml" var="xml" />
      <x:parse xml="${xml}" var="itemlist" scope="application" />
     </c:if>
    
     <c:if test="${applicationScope.itemlist == null}" >
      <p>Unable to find or parse /WEB-INF/itemlist.xml<br>
     </c:if>
    

Creating the showDetail.jsp page

  1. Create a new JSP file:
    1. Choose File New.
    2. Select Java Server Page and name it showDetail.jsp.
    3. Accept the defaults and click Next and then Finish.
  2. At the end of showDetail.jsp, add the following lines after the existing tag declarations:
    <html>
    <head><title>Item Details</title>
    </head>
    <body>
    
    <%-- include will re-parse xml if needed (e.g., came here by bookmark) --%>
     <jsp:include page="parseItemList.htm" />
    
     <%-- Select only the item with the id attribute matching query string id --%>
     <x:set var="itemSel" select="$itemlist/items/item[@id=$param:id]"/>
      <h3>
       <x:out select="$itemSel/mfg"/>
       <x:out select="$itemSel/model"/>
       <x:out select="$itemSel/name"/>
    </h3>
    
     <p><x:out select="$itemSel/description"/>
    
     <%-- Output bulleted list of each bullet element under selected item --%>
     <ul>
     <x:forEach var="bullet" select="$itemSel/bullet">
      <li><x:out select="$bullet" /></li>
     </x:forEach>
     </ul>
    
     <p>Shipping weight: <x:out select="$itemSel/ship/weight"/>
       <x:out select="$itemSel/ship/weight/@unit"/>
    
     <c:set var="shipLoc">
      <x:out select="$itemSel/ship/loc"/>
     </c:set>
    
     <p>Usually ships within
     <c:choose>
      <c:when test="${shipLoc == 'warehouse'}" >
       24 hours
      </c:when>
      <c:when test="${shipLoc == 'depot'}" >
       1-2 days
      </c:when>
      <c:otherwise>
       1-2 weeks
      </c:otherwise>
    </c:choose>
    
     <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>
    
     </body>
    </html>
    

Creating the XML data file

  1. Create a new XML file:
    1. Choose File New.
    2. Select XML File, name it items.xml, and use the Browse button to set the location to C:\AppDevStudio\webAF\Projects\JSTL_EL\webapp\WEB-INF (assuming you installed AppDev Studio in the default location).
    3. Accept the default of Blank XML file and click Next and then Finish.
  2. In items.xml, add the following lines:
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    
    <items>
     <item id="SH-001">
      <mfg>SkyHiTech</mfg>
      <model>SL-100S</model>
      <name>SateLighter</name>
      <description>Cigarette lighter with integrated GPS</description>
      <bullet>Automatically locates nearest cigarette vending machine</bullet>
      <bullet>Serves as a conventional cigarette lighter too</bullet>
      <bullet>Requires 9V battery (not included)</bullet>
      <price>229.95</price>
      <ship qty="each">
       <weight unit="oz">14</weight>
       <loc>depot</loc>
       <stock>13</stock>
      </ship>
     </item>
     <item id="NR-021">
      <mfg>NeverReady</mfg>
      <model>NR-900X</model>
      <name>-9 Volt battery</name>
      <description>9V Battery w/ reversed polarity markings</description>
      <bullet>Suitable for all devices needing minus 9 volts</bullet>
      <price>3.95</price>
      <ship qty="each">
       <weight unit="oz">4</weight>
       <loc>warehouse</loc>
       <stock>23</stock>
      </ship>
     </item>
     <item id="GS-001">
      <mfg>NetScrape</mfg>
      <model>VCO-01</model>
      <name>Virtual Can Opener</name>
      <description>Internet-ready can opener</description>
      <bullet>Opens cans downloaded from the World Wide Web</bullet>
      <bullet>Not for use with jars</bullet>
      <price>5.95</price>
      <ship qty="each">
       <weight unit="oz">11</weight>
       <loc>drop</loc>
       <stock>-3</stock>
      </ship>
    
     </item>
    
    </items>
    

Testing the Web application

  1. Choose File [arrow] Save all.
  2. Choose Build [arrow] Build project.
  3. Choose Tools [arrow] Services [arrow] Start Java Web Server.
  4. Wait a moment to allow the web server to initialize. Then click the Execute in browser button to display the page.