Develop custom JSP behaviors (1)

Source: Internet
Author: User
Tags tld

1. To develop a custom behavior, you need to use a series of classes and interfaces, which are called the tag extension mechanism in JSP1.1 specifications ). All interfaces and classes required to implement the tag Handler are defined in the javax. servlet. jsp. tagext package. The two main interfaces are Tag and BodyTag. To make the development of the tag handler easier, the API defines two supported classes: TagSupport and BodyTagSupport. These two classes provide the default implementation for the above two interfaces respectively.
2. The tag library is a set of custom rows. In addition to marking handler class files, the tag library must also include a tld file. This is an XML file that maps all custom row names to corresponding tag handler classes and describes all attributes supported by each custom behavior. Class files and tld files can be packaged into a JAR file for easy installation.
3. Development, configuration, and use of a simple custom behavior usually need to do this work.
1) implement a tag handler class. Compile the class and place the generated class file under the WEB-INF/classes directory of the application.
2) create a TLD file. Let's look at the simple example below.

PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.1 // EN"
Http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd>

1.0
1.1
Test

Hello
Com. mycompany. HelloTag
Empty

Name



Name the file *. tld and place it in the application's WEB-INF/tlds directory.
3) Now you can use custom behaviors on the JSP page.
<% @ Taglib uri = "/WEB-INF/tlds/mylib. tld" prefix = "test" %>





When this page is requested, the JSP Container uses this TLD to find the class corresponding to the custom behavior. It then calls all corresponding methods and adds the corresponding text to the response. The above is all we have to do in the simplest case.
4. First, let's take a look at the most important Tag interface method:
Public void setPageContext (PageContext pageContext );
Public int doStartTag () throws JspException;
Public int doEndTag () throws JspException;
Then, let's take a look at the implementation of these methods provided by the TagSupport class.
Public class TagSupport implements Tag, Serializable {
Protected PageContext pageContext;
.......
Public void setPageContext (PageContext pageContext ){
This. pageContext = pageContext;
} // This method is called by the JSP Container before the tag handler is used.
Public int doStartTag () throws JspException {
Return SKIP_BODY;
} // When the start flag is encountered, the JSP Container will call the doStartTag () method.
Public int doEndTag () throws JspException {
Return EVAL_PAGE;
} // When the end sign is reached, the JSP Container will call the doEndTag () method.
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.