Javaweb Basic-JSP Custom tags Getting Started

Source: Internet
Author: User
Tags tld

Customizing the role of Tags: replacing Java code for JSP pages

Step : 1. Label processing class (the label is an object, it needs to have a class first)

2.tld file It is an XML (can be borrowed from the C tag), generally put under Web-inf, do not let the client browser see
Core Configuration <tag>
<name>myTag</name>
<tag-class>com.jiangbei.tag.MyTag</tag-class>
<body-content>empty</body-content>
</tag>
"Notice the corresponding class inside the Tag-class remember to change!" 】
3. Specify the TLD file path using <% @taglib%>

For example:

Label Processing classes:

 Public classMyTagImplementssimpletag{PrivatePageContext PageContext; Privatejspfragment body; @Override Public voidDotag ()throwsjspexception, IOException {//want to output to the page, need out,pagecontext a top nine, can get outPagecontext.getout (). Print ("Hello World"); } @Override PublicJsptag getParent () {//TODO auto-generated Method Stub        return NULL; } @Override Public voidsetjspbody (jspfragment jspbody) { This. BODY =Jspbody; } @Override Public voidsetjspcontext (Jspcontext pc) {//The server sent it over to save him .         This. PageContext =(PageContext) PC; } @Override Public voidsetParent (jsptag parent) {//TODO auto-generated Method Stub            }}

Create TLD files in this directory

The contents of the file are as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><taglib xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http:/ /www.w3.org/2001/xmlschema-instance "xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version= "2.1" > <tlib-version>1.0</tlib-version> < Short-name>itcast</ Short-name> <uri>http://www.itcast.cn/tags/1.0</uri><tag> <name>myTag</name> <tag-class>com.jiangbei.tag.mytag</tag-class> <body-content>empty</body-content> </tag> <tag> <name>mytag2</na Me> <tag-class>com.jiangbei.tag.mytag2</tag-class> <body-content>empty</body-content> </tag> <tag> <name>mytag3</na Me> <tag-class>com.jiangbei.tag.mytag3</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>myta G4</name> <tag-class>com.jiangbei.tag.mytag4</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</n Ame> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag></taglib>

Directly inheriting the class and avoiding the direct implementation of the interface:

  

 Public classMyTag3extendssimpletagsupport{@Override Public voidDotag ()throwsjspexception, IOException {//gets the stream, in order to execute the parameter outputWriter out = This. Getjspcontext (). Getout (); Out.write ("********************<br/>"); //executes the label body content, writes to the specified stream (on the page)         This. Getjspbody (). Invoke (out); Out.write ("<br/>********************"); }    }

label processing class (Jspfragment jsp fragment, which is content)

dotag () each time the label is executed, the method is called. will be called by Tomcat after other methods
set/getparent the method of manipulating the parent tag, where get is a non-declarative periodic method
setjspbody (jspfragment jspbody)
void Setjspcontext (Jspcontext pc)
of course, sometimes it's inconvenient to implement interfaces, so there's a class for us to inherit
simpletagsupport, has helped us to save everything, and provides a Get method
for subclass invocation
tag body content supported types
empty
jsp: Not supported! (The tag body content can be Java script, El expression, etc.)
scriptless is an El expression (and of course ordinary strings are also available)
fourth kind of useless!

You can use the throw new skippageexception in the label Handling Class Dotag () method,
Tomcat recognizes this tag and it captures it. The back will never be executed again.
Work directory to see what the true

You can add attributes to tags:
Adding attributes to a label processing class
Configure the properties in the TLD file (see TLDs itcast_tld under Web-inf)
Rtexprvalue: Run-time expression value, that is, can be an El expression value

Javaweb Basic-JSP Custom tags Getting Started

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.