Section 15.4. Working with the JSTL

   

15.4 Working with the JSTL

The JavaServer Pages Standard Tag Library has been under development for some time (more than two years to arrive at version 1.0). It answers many of the concerns you may have had upon learning that JSP had only a few core tags. The purpose of the JSTL is to provide standard iteration, conditional evaluation, XML processing, database access, and expression-language tags.

While there sadly is not world enough and time to go into the JSTL in detail, you're a ColdFusion developer, so I don't think we'll need to. What you need to know is how to get it and install it. Once you've got it, it is very easy to poke around and start using it. But you need to do these things to start using JSTL:

  1. Download the JSTL from http://jakarta.apache.org/ taglibs /doc/standard-doc/intro.html.

  2. Make the JSTL classes available to the server. This is easily done by placing them in your Web application's WEB-INF/lib directory.

  3. Put the TLD files into your WEB-INF directory or a subdirectory thereof.

  4. Download and install an XML parser. The JSTL requires this, and JSP 1.2 does not. That means that you may not have one of these installed on your system, and you'll need to get one that supports both SAX and DOM. It is a good idea for portability's sake that you include this parser with your Web application distribution. It is easy and free to use the Apache Xerces-J parser, which can be downloaded from http://xml.apache.org/ xerces-j .

Once you have downloaded and installed the JSTL, it is a good idea to look at the source and read the included docs to see what is available to you. There are tags for setting variables (instead of <cfset var="value"> , you can use <c:set var="Value"/> ), and instead of < cfswitch > and <cfcase> , you use <c:choose> and <c:when> .

Let's look at an example now. The following code takes a parameter that defines an XSL file to be used. When the value of that parameter is determined, then the appropriate XML document is selected. This setup, while it may seem backwards , allows us to easily use partial views of the same XML document set for getting the specific content we want. Once we have both the XML and XSL files, we transform them using the JSTL <x:transform> tag in the following listing, makeContent.jsp .

15.4.1 xmlTransformerSwitch.jsp

 <%--File: xmlTransformerSwitch.jsp      Purpose: switch against url param xsl. Use this as the         value of the xsl file for the transformation of         content. Certain xsl files go with the few xml files,         so those couplings are coded into the same cases. The         benefit here is that you can add a touch of JSP code to         check for the client and use a different XSL sheet to         transform the content into WAP/WML output, for instance --%> <% //set the path to the files for reuse    String xmlPath = "webapps/mycontext/WEB-INF/xmlcontent/";    String xslPath = xmlPath + "xslt/"; %>     <c:set var="theXSL">        <%= xslPath + request.getParameter("style") + ".xsl" %>     </c:set>     <c:choose>         <c:when test="${param.xsl == 'contactus'}">             <c:set var="theXML">                 <%= xmlPath + "company.xml" %>             </c:set>         </c:when>         <c:when test="${param.xsl == 'aboutus'}">             <c:set var="theXML">                 <%= xmlPath + "company.xml" %>             </c:set>         </c:when>         <c:otherwise>             <c:set var="theXML">                 <%= xmlPath + "products.xml" %>             </c:set>         </c:otherwise>     </c:choose> 

This code performs a switch against the value passed in the style parameter. Once the parameter value is determined, it sets a path to a different XML file that will be transformed. This file is called by the makeContent.jsp , shown below.

15.4.2 makeContent.jsp

 <%--File: makeContent.jsp      Parameters: ?style=somexsldoc     Use: page displays middle content area. xml data is        transformed into html via documents stored under        WEB-INF/xmlcontent.     Purpose: to support display of data on multiple devices. --%> <%@ page language="java" contentType="text/html" %> <%@ page errorPage="errorsgalore.jsp" %>  <!--get JSTL core library-->   <%@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core" %>   <!--get JSTL for XML -->   <%@ taglib prefix="x" uri="http://java.sun.com/jstl/ea/xml" %>  <jsp:include page = "header.jsp" /> <tr>     <td width="25"><img src="images/spacer.gif" width="25"/>     </td>     <td valign="top" width="400">      <span class="txt">  <%--switch determines what xml and xsl file to use--%>   <%@ include file="xmlTransformerSwitch.jsp" %>   <%--perform transformation--%>   <x:transform xmlUrl="${theXML}" xsltUrl="${theXSL}" />  </span>     </td>    </tr> <jsp:include page="footer.jsp" /> 

This file calls the JSTL <x:transform> tag to run an XML source and an XSLT file through the transformer together, to output an HTML result. A different XML file is used depending on the value of the XSL "style" param determined in the xmlTransformerSwitch.jsp above.

A sample XSL file that serves as the blueprint for the transformation is shown here. This simple file is aboutus.xsl and finds corresponding elements in an XML document to match and apply the style to.

15.4.3 aboutus.xsl

 <?xml version="1.0" encoding="UTF-8"?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!--ABOUTUS.xsl--> <!-- import global variables--> <xsl:import href="globals.xsl" /> <xsl:output method="xhtml"indent="yes" encoding="iso-8859-1"/> <!--match root. select aboutus text only--> <xsl:template match="/">     <xsl:copy>         <xsl:apply-templates select="/company/aboutus" />     </xsl:copy> </xsl:template> <!-- header and text--> <xsl:template match="aboutus">     <img src="images/aboutUsHeader.gif" alt="about us" />     <p><span class="greytext"><xsl:apply-templates /></span>     </p> </xsl:template> </xsl:stylesheet> 

These examples demonstrate how easy it can be to start doing sophisticated things with the JSTL especially if you are a seasoned ColdFusion developer.

15.4.4 taglib for the JSTL

Here is a sample description used in a web.xml for the JSTL.

 ...  <!--JSP Standard Tag Library 1.0--> <taglib>   <taglib-uri>http://java.sun.com/jstl/ea/core</taglib-uri>   <taglib-location>/WEB-INF/jstl10/tld/c.tld</taglib-location>   </taglib>   <taglib>   <taglib-uri>http://java.sun.com/jstl/ea/core-rt</taglib-uri>   <taglib-location>/WEB-INF/jstl10/tld/c-rt.tld   </taglib-location>   </taglib>   <taglib>     <taglib-uri>http://java.sun.com/jstl/ea/xml</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/x.tld     </taglib-location>   </taglib>   <taglib>     <taglib-uri>http://java.sun.com/jstl/ea/xml-rt</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/x-rt.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>http://java.sun.com/jstl/ea/fmt</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/fmt.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>http://java.sun.com/jstl/ea/fmt-rt</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/fmt-rt.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>http://java.sun.com/jstl/ea/sql</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/sql.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>http://java.sun.com/jstl/ea/sql-rt</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/sql-rt.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>/jstl-examples-taglib</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/jstl-examples.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>http://jakarta.apache.org/taglibs/standard/          scriptfree</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/scriptfree.tld</taglib-location>   </taglib>   <taglib>     <taglib-uri>http://jakarta.apache.org/taglibs/standard/          permittedTaglibs</taglib-uri>     <taglib-location>/WEB-INF/jstl10/tld/          permittedTaglibs.tld</taglib-location>   </taglib> </web-app> 

The folder name jstl10 is arbitrary: It's the name you use when you unzip the JSTL download from Apache.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net