Java Reference
In-Depth Information
JSTL tags are not as simple. When a JSTL tag is added, RAD not only inserts the tag into the source code but also
inserts a taglib (tag library) directive. The directive identifies the file (i.e., tag library) that contains the tag definition.
The tag definition specifies which tag handler class should be invoked for the tag. For instance, when the JSTL if tag
was added to the JSP, RAD added the following taglib directive:
<%@taglib uri=" http://java.sun.com/jsp/jstl/core " prefix="c"%>
The URI (Uniform Resource Identifier) value can be an actual file within the project such as /WEB-INF/tls/TNT.
tld or an alias. In this chapter, you will create a folder named tls in WEB-INF and then, within tls, a tag library file
called TNT.tld. A tag library URI (i.e., an alias) of http://www.tnt.com/taglib will then be created and associated
with /WEB-INF/tls/TNT.tld. This association is defined in a project file called the Web Deployment Descriptor .
Notice that the taglib directive also defines a prefix of c for the JSTL tags. This means that when a JSTL tag
(that is defined in the jstl/core tag library) is inserted in the page, the JSTL tag keyword must begin with the letter c
followed by a colon. In other words, the prefix identifies which directive the server should use to locate the tag library
associated with this tag.
In fact, RAD inserted the following JSTL tags when the if tag was added to the JSP:
<c:if test=""></c:if>
The actual JSTL tag keyword is if but notice that it is preceded by c: . So, in this example, the c: tells the server
that the tag library is located at http://java.sun.com/jsp/jstl/core . The server then goes to the Web Deployment
Descriptor to get the actual location of the tag library.
Got it? Let's recap. When the server encounters a JSP tag in the page, the server:
A.
Reads the tag prefix
B.
Within the JSP, finds the tag library directive associated with the tag prefix
C.
Using the directive's URI, goes to the Web Deployment Descriptor and retrieves the tag
library file name and location
D.
Goes to the taglib file and retrieves the name and location of the tag handler class
associated with the tag
E. Runs the tag handler class
With the if tag, the tag handler evaluates a condition. If the condition is true, the body of the tag will be
embedded into the response object. For example, if the body contained the text “Yowser!” and the condition was true,
the text “Yowser!” would be embedded in the page and appear in the browser.
Who cares? Well, we do. We need a simple tag to create and populate the Employee bean from the JSP fields.
In addition, we are also going to create a tag that creates a specialized state choice field that limits the user to only valid
state abbreviations. By creating a state field tag, we will be able to use it on multiple JSPs rather than defining a state
choice field for each JSP. More important, if a state must be added to or deleted from the list, only the tag handler will
have to be changed, not every JSP that displays the state value. Finally, the application will be modified to display the
tax amount and gross salary for an employee on the same page as all the other employee information.
Creating a Custom Tag
As mentioned, we will create a custom tag (getEmp) to replace the following bean tags in EnterEmpInfoJSP:
<jsp:useBean id="EmpBean" class="c9java.Employee" scope="request">
<jsp:setProperty name="EmpBean" property="empStreet"
param="streetAddrTF" />
<jsp:setProperty name="EmpBean" property="exemptions" param="exmpDDM" />
 
Search WWH ::




Custom Search