Main Page

Previous Section Next Section

Putting It Together

The JSP in Listing B.1 shows some of the XML tags we have discussed, and Figure B.1 shows the corresponding output.

Click To expand
Figure B.1: The outout from the JSTL example
Listing B.1: An example JSP using JSTL XML tags
Start example
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>

<!-other JSP code->
<x:parse var="catalog" scope="application">
    <items>
        <item>
            <quantity>3</quantity>
            <productnumber>229AXH</productnumber>
                <description>High speed photocopier machine with automatic sensors
                                                                    </description>
            <unitcost>1939.99</unitcost>
        </item>
        <item>
            <quantity>1</quantity>
            <productnumber>1632</productnumber>
            <description>One box of color toner cartridges</description>
            <unitcost>43.95</unitcost>
        </item>
    </items>
</x:parse>
<b> forEach example </b>
c Here is the value stored in the "parseditems" variable
<x:forEach var="item" select="$applicationScope:catalog/items/*">
<br>     Item quantity: <x:out select="$item/quantity"/>
<br>     Item product number: <x:out select="$item/productnumber"/>
<br>     Item product number: <x:out select="$item/productdescription"/>
<br>     Item cost:$  <x:out select="$item/unitcost"/>
<br><br>
</x:forEach>
<hr>

<b> if example </b> <br>
<x:forEach var ="item" select="$applicationScope:catalog/items/*">
     <x: if<D>  select="$item/unitcost >1000">
     This item has a cost greater than $1000: <x:out select="$item/description"/>
     </x:if>
</x:forEach>

<hr>
<b> choose-when example </b><br>
<x:forEach var="item" select="$applicationScope:catalog/items/*">
<x:choose>
     <x:when  select="$item/unitcost >1000">
         This item has a cost greater than $1000: $ <x:out select="$item/unitcost"/>
         <br>  <x:out select="$item/description"/>
     </x:when>

     <x:when   select="$item/unitcost < 100">
          This item has a cost less than $100: $ <x:out select="$item/unitcost"/>
          <br> <x:out select="$item/description"/>
     </x:when>
</x:choose>
</x:forEach>
<hr>
<b> otherwise example </b> <br>
<x:forEach var="item" select="$applicationScope:catalog/items/*">
<x:choose>
     <x:when   select="$item/unitcost < 10">
          This item has a cost less than $10: $ <x:out select="$item/unitcost"/>
         <br> <x:out select="$item/description"/>
     </x:when>
     <x:otherwise>
        This item is more than $10 <x:out select="$item/description"/>
        <br>
     </x:otherwise>
</x:choose>
</x:forEach>
End example

Previous Section Next Section


JavaScript Editor Java Tutorials Free JavaScript Editor