Java Reference
In-Depth Information
only use the specific syntax in the useBean tag, and it does the rest (getting
and storing) for you.
19.3.7
Well, we're almost done with JSP, but the one topic that we have yet to cover
is huge. It's the trap door, or the way back out, through which JSP can get to
lots of other code without the JSP author having to write it. Tag libraries are
specially packaged libraries of Java code that can be invoked from within the
JSP. Just like the useBean , they can do a lot of work behind the scenes and
just return the results.
There are lots of available libraries, which is one reason for this topic to
be so huge. We could spend chapters just describing all the various database
access routines, HTML generating routines, and so on available to you. Perhaps
the leading tag library is the JSP Standard Tag Library ( JSTL).
Here are two of the most common directives used with tag libraries. First
is the directive that declares a library to be used:
Tag Libraries
<%@ taglib prefix="my" uri="http://java.sun.com/jstl/core" %>
You then use the prefix as part of the tag name on subsequent tags that
refer to this library. For example, if we had an out directive in our library, we
could use my as the prefix, separated by a colon: <my:out ...> .
The second directive we will show is a for loop. The for loop mechanism
provided by this library is in some ways simpler than using Java scriptlets. It
comes in many forms, including one for explicit numeric values:
<my:forEach var="i" begin="0" end="10" step="2">
This example will loop six times with i taking on the values 0 , then 2 ,
then 4 , and so on. Another variation of the forEach loop can also make it easy
to set up the looping values:
<my:forEach var="stripe" items="red,white,blue">
In this example it will parse the items string into three values: red ,
white , and blue , assigning each, in turn, to the variable stripe . In fact the
items attribute can also store an array, or collection, or iterator from the Java
code that you may have declared (or that is implicit from the underlying
Search WWH ::




Custom Search