Skip to main content

More Easy JSTL

Posted by bleonard on September 30, 2005 at 11:34 AM PDT

Yesterday I put together a blog showing how easy it is to work with JSTL in NetBeans. One thankful reader asked for a follow-up on using the JSTL XML library. I think this was primarily due to a problem he was having with a package name change from JDK1.4 to 5 (see my response for more details). But it's simple enough, so here goes anyhow. I'll basically just do what I did yesterday, display a table of customers. However, today, that customer list will come from and XML document rather than a SQL database.

<?xml version="1.0" encoding="UTF-8"?>
<customers>
    <customer>
        <num>1</num>
        <name>SuperCom</name>
        <city>Miami</city>
        <phone>305-777-4632</phone>
    </customer>
    <customer>
        <num>2</num>
        <name>Livingston Enterprises</name>
        <city>Miami</city>
        <phone>305-456-8888</phone>
    </customer>
    <customer>
        <num>25</num>
        <name>Oak Computers</name>
        <city>Dallas</city>
        <phone>214-999-1234</phone>
    </customer>
    <customer>
        <num>3</num>
        <name>MicroApple</name>
        <city>Atlanta</city>
        <phone>555-275-9900</phone>
    </customer>
</customers>

I didn't mention yesterday, but this exercise was done with NetBeans 4.1. You could also use the NetBeans 5.0 BETA .

  1. Start NetBeans
  2. Create a new Web Application named CustomerListXML (File > New Project > Web > Web Application)
  3. Add the JSTL library to the project (Properties > Libraries > Add Library)
  4. Uncomment the <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> line in index.jsp
  5. Add the line <%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
  6. Insert the following between the tags:
 <h1>Customer List</h1>
  
<!-- Parse the XML Customer List -->
<c:import url="customers.xml" var="xml"/>
<x:parse xml="${xml}" var="customers"/>

<TABLE border=1>
     <TR>
         <TD>Customer Num</TD>
         <TD>Customer Name</TD>
         <TD>Customer City</TD>
         <TD>Customer Phone</TD>
     </TR>
     <x:forEach select="$customers/customers/customer" var="customers">
         <TR>
             <TD><x:out select="num"/></TD>
             <TD><x:out select="name"/></TD>
             <TD><x:out select="city"/></TD>
             <TD><x:out select="phone"/></TD>
         </TR>
     </x:forEach>
</TABLE>
  1. If you actually took the time to type in the code, you should have noticed the very helpful JSTL code assistance and documentation:



    jstldoc.PNG

  2. If you pasted in the code from above, Press Ctrl+Shift+F to reformat the code.
  3. Add a new Well-formed XML Document named "customers" to the Web Pages folder (New > File/Folder > XML Documents) and replace the contents with the XML from above.
  4. Now, if you're developing on JDK 1.4 OR using the J2EE 1.4 RI (Sun Java System Application Server 8.1) as your runtime, press F6 to test your application. If you're developing on JDK 5.0 AND running on Tomcat, you also need to add xalan.jar to your project (Project > Libraries > Add JAR/Folder) before pressing F6 to run it.



    result-xml.png
Related Topics >>