Introduction to using instances Based on JSP custom tags

Source: Internet
Author: User
Tags return tag tld

Add a JSP custom tag:

Add a tld file to the WEB-INF folder first
<? Xml version = "1.0" encoding = "UTF-8"?>
<Taglib 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 web-jsptaglibrary_2_0.xsd"
Version = "2.0">

<Description> myTag </description>
<Display-name> JSTL core </display-name>
<Tlib-version> 1.0 </tlib-version>
<Short-name> cnweb </short-name>
<Uri> http://www.cnweb.cn </uri>

<Tag> <! -- A tag corresponds to a custom tag class -->
<Description> MyTag </description>
<Name> when </name>
<Tag-class> cn. example. when </tag-class>
<Body-content> scriptless </body-content>
<Attribute>
<Name> test </name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
</Taglib>
Reference rules:
<% @ Taglib uri = "http://www.cnweb.cn" prefix = "cnweb" %>

Define error handling page:

<Error-page>
<Exception-type> java. lang. Exception </exception-type>
<Location>/errors/error. jsp </location>
</Error-page>
<Error-page>
<Error-code> 404 </error-code>
<Location>/errors/error1.jsp </location>
</Error-page>

Custom tag execution process:

SimpleTagSupport

/* The Jsp Engine experiences a simple class, which is instantiated;
* Call setJspContext to pass the page pageContext to the tag processing class;
* Call setParent to pass the parent tag. If no, null is passed.
* Call the setJspBody method to pass the encapsulated label JspFragment to the label processor class.
* The custom tag and doTag () method on the execution page; --> the execution is complete, and the object is destroyed.
*/

JspFragment jf = this. getJspBody ();
Jf. invoke (this. getJspContext (). getOut (); // if not displayed, no processing is performed.

--------------------------------------------------------------------

Public classTagShowOrNotExtends TagSupport {
Public int doStartTag () throws JspException {
Return Tag. EVAL_BODY_INCLUDE; // display the body
// Return Tag. SKIP_BODY; // hide the body
// Tag. EVAL_PAGE; // display page
// Tag. SKIP_PAGE; // hide the page
}}

TagSupport
/* Write a class that implements TagSupport;
* Describe the tag processor class in the tld file (tld File Location: Under the WEB-INF)
* Import and use tags on the jsp page
*
* This class is instantiated when a custom tag is encountered during jsp execution;
* Then run setPageContext () --> setParent () --> doStartTag ()
* If there is a TAG body, the TAG body is generally executed; doEndTag () --> after the entire tag is executed, it is generally executed: release ()
*
* Determines whether the entire jsp page is executed;
* Controls whether a part of the jsp page is executed;
* Control repeated execution of jsp page content;
* Modify jsp page content output;
*/

// After the label body is executed, the method is executed before the doEndTag () method is executed until the method returns IterationTag. SKIP_BODY;
Public int doAfterBody () throws JspException {
Time --;
System. out. println ("repeated" + time );
If (time> 0 ){
Return IterationTag. EVAL_BODY_AGAIN;
} Else
Return IterationTag. SKIP_BODY;
}

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.