Develop JSP custom Behaviors

Source: Internet
Author: User
Tags tld

1. To develop a custom behavior, you need to use a series of classes and interfaces called tag extension mechanisms 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.
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE taglib
PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.1 // EN"
Http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd>
<Taglib>
<Tlibversion> 1.0 </tlibversion>
<Jspversion> 1.1 </jspversion>
<Shortname> test </shortname>
<Tag>
<Name> hello </name>
<Tagclass> com. mycompany. HelloTag </tagclass>
<Bodycontent> empty </bodycontent>
<Attribute>
<Name> name </name>
</Attribute>
</Tag>
</Taglib>
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" %>
<Html>
<Body bgcolor = "white">
<Test: hello name = "maojb"/>
</Body>
</Html>
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.

Related Article

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.