Download tag

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JSP in Action
continuation
JSP’s Tag Extension Mechanism
• You can define your own actions to replace lengthy scriptlets
• By “hiding” functions behind custom tags,
• you can increase modularity and maintainability of your pages.
1. Define Java classes that provide the functionality of the new
actions, including the definition of their attributes (e.g.,
myAttributeName). These classes are called tag handlers.
2. Provide a formalized description of your action elements, so that
Tomcat knows how to handle them. For example, you need to
specify which actions can have a body and which attributes can be
omitted. Such a description is called a tag library descriptor (TLD).
3. In the JSP pages, tell Tomcat that the pages need your tag library
and specify the prefix that you want to identify those custom tags
with.
• Bodyless Custom Action
• <wow:weekday date=“date”/>
Step 1: Define the Tag Handler
• A tag handler for a bodyless custom tag is a class that implements the
interfaces java.io.Serializable and javax.servlet.jsp.tagext.Tag.
• Remember that to satisfy an interface, you have to implement all the
methods it defines.
• To satisfy Serializable, you only need to define a unique identifier, like
this:
static final long serialVersionUID = 1L;
Step 2: Define the TLD
Step 3: Use the Custom Action
Bodied Custom Actions
Step 1: Define the Tag Handler
• Bodied actions need to implement an interface,
javax.servlet.jsp.tagex.BodyTag instead of Tag.
• Implement doEndTag method,and doAfterBody
<wow:weekdayBody></wow:weekdayBody>
Step 2: Define the TLD
Step 3: Use the Custom Action
Tag Files
• Tag files are special JSP files that replace tag handlers written in Java.
• There are three directives: tag, attribute, and variable.
Bodyless Tag
• The tag directive of a tag file replaces the page directive of a JSP page,
and the attribute directive lets you define an input parameter.
• Another simplification is in sending the result to the output, because
in the tag file the implicit variable out makes it unnecessary to invoke
pageContext.getOut().
Bodied Tag
• If you omit the attribute var, doBody sends the body’s result to the output;
• If you replace var with varReader, the result is stored as a java.io.Reader
object instead of a java.lang.String;
• If you add the attribute scope, you can specify var/varReader to be defined
as a request, session, or application attribute, instead of in the page scope.
• Know that jsp:invoke is very similar to jsp:doBody but operates on a JSP
fragment instead of the action body.
• For example, by writing the following two lines in a tag file
<%@attribute name="fragName" fragment="true"%>
<jsp:invoke fragment="fragName"/>
The tag Directive
The attribute Directive
The variable Directive
JSTL and EL Next Meeting