J2EE custom tag details

Source: Internet
Author: User
Tags tld

1. Several important components of custom tags:
Inherited class: javax. servlet. jsp. tagext. *: tagsupport
One attribute: protected pagecontext
Two Methods: Public int dostarttag ()
Public int doendtag ()
Three constants: skip_body, skip_page, and eval_page
Skip_page, eval_page is the return value of the doendtag () method.
Tag library description file (*. TLD)
2. Process (Tool: Eclipse ):
1. Create a WEB Project
2. Create a new package under the project, create a class in the package, and inherit the tagsupport
3. Complete tag Processing

Package demo. Tag;
Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp .*;
Import javax. servlet. jsp. tagext. tagsupport;
Import java. Io .*;
// Several frequently used packages to be imported
@ Suppresswarnings (\ "Serial \")
Public class tagdemo extends tagsupport ...{
Private int pagesize; // The pagesize attribute, which controls the number of pages displayed and generates get and set methods.
Public int getpagesize ()...{
Return pagesize;
}
Public void setpagesize (INT pagesize )...{
This. pagesize = pagesize;
}
// Public int doendtag () throws jspexception {
// It is useless here
// Return Super. doendtag ();
//}
Public int dostarttag () throws jspexception ...{
Jspwriter out = pagecontext. getout (); // use pagecontext to get out. It can also get session and so on. Basically, it can be obtained from embedded JSP objects, which is very useful.
For (INT I = 0; I <pagesize; I ++ )...{
Try ...{
Out. println (\ "<Table> \");
Out. println (\ "<tr> \");
Out. println (\ "<TD> \"); [Page]
Out. println (I );
Out. println (\ "</TD> \");
Out. println (\ "<TD> \");
Out. println (I * 10 );
Out. println (\ "</TD> \");
Out. println (\ "</tr> \");
Out. println (\ "</table> \");
} Catch (ioexception e )...{
E. printstacktrace ();
}
}
Return skip_body;
}
}
 

1. The tag processing class should inherit from the tagsupport class.
2. the tag processing class should overwrite at least one method of the parent class, dostarttag () or doendtag (). If you overwrite dostarttag (), skip_body should be returned. If you overwrite doendtag () method should return skip_page, eval_page
3. Make full use of the pagecontext attribute of the tag processing class. You can use this attribute to obtain various objects on the JSP page using custom tags:
Getout (): Get the output stream out of the JSP page
Getrequest: Get the request object request of the JSP page
Getsession: obtains the session object session of the JSP page.
Getservletcontext: obtains the application object application [Page] of the JSP page.
4. If we want to complete a custom tag with attributes, We need to declare the relevant attributes in the tag processing class.
5. Create a New. TLD file under WEB-INF to complete the tag library description file
 

<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?>
<! Doctype taglib
Public \ "-// Sun Microsystems, Inc. // dtd jsp tag library 1.2 // en \"
\ "Http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd\">
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 1.2 </JSP-version>
<Short-Name> mytaglib </short-Name>
<Tag>
<Name> showdatatag </Name>
<Tag-class> demo. Tag. tagdemo </Tag-class>
<Attribute>
<Name> pagesize </Name>
<Required> true </required>
<Type> integer </type>
</Attribute>
</Tag>
</Taglib>

1. The tag library description file must be suffixed with TLD.
2. The tag library description file should comply with the XML syntax requirements.
3. The root element of the tag library description file must be <taglib>
4. The following four child elements must be completed under the taglib root element:
Tlib-version: JSP-version: Short-Name: Brief description of the tag library description file, which can only appear once
Tag: it can appear one or more times. It describes the tag name in the tag Library and the tag processing class you want.
5. sub-elements under the tag element:
Name sub-element: Label name
Tag-class sub-element: Corresponding tag processing class
Attribute sub-element: attributes that the label should contain
6. child elements under attribute:
Name: attribute name
Required: This attribute name cannot be omitted.
Type: the type of related attributes in the tag processing class.
5. Introduce the tag library description file in Web. xml

<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> [Page]
<Web-app version = \ "2.4 \"
Xmlns = \ "http://java.sun.com/xml/ns/j2ee \"
Xmlns: xsi = \ "http://www.w3.org/2001/xmlschema-instance \"
Xsi: schemalocation = \ "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\ ">
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
<JSP-config>
<Taglib>
<Taglib-Uri> myfirsttag </taglib-Uri>
/WEB-INF/mytaglib. TLD </taglib-location>
</Taglib>
</JSP-config>
</Web-app>

1. implement sub-elements of the taglib element in Web. xml:
Taglib-location: relative path of the tag library description file in the Web Application
/WEB-INF/mytaglib. TLD </taglib-location>
Taglib-Uri: the name of the tag library called in JSP
<Taglib-Uri> ABC </taglib-Uri>
2. Declare in JSP of the tag library to be called
Declaration: <% @ taglib uri = \ "myfirsttag \" prefix = \ "suibian \" %>
Call: <suibian: showdatatag pagesize = \ "10 \"> </suibian: showdatatag>

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.