Javaweb Learning Summary (24)--jsp Traditional label development

Source: Internet
Author: User
Tags closing tag control label return tag tld

API for Label Technology1.1, Tag technology, API class inheritance relationship

  

Second, the label API Simple Introduction 2.1, Jsptag interface

  The Jsptag interface is the parent interface for all custom tags and is a newly defined markup interface in JSP2.0 with no properties and methods . Jsptag interface has tag and Simpletag two direct sub-interface, JSP2.0 previous version only the tag interface, so the implementation of the tag interface Custom label is also called the traditional label , A custom label that implements the Simpletag interface is called a simple label .

2.2. Tag interface

  The tag interface is the parent interface for all traditional tags, which defines two important methods (doStartTag, Doendtag) methods and four constants (Eval_body_include, Skip_body, Eval_page, Skip_page) , these two methods and the four constants function as follows:

(1) When the Web container interprets the execution of the JSP page, it encounters the start tag of the custom tag and invokes the doStartTag method of the label processor, which can return the constant eval_body_include or skip_ to the Web container after the doStartTag method has finished executing. BODY. If the doStartTag method returns the Eval_body_include,web container, the label body of the custom label is then executed, and if the doStartTag method returns the Skip_body,web container ignores the label body of the custom label. The end tag that directly interprets the execution of the custom label.

(2) When the Web container interprets the end tag that executes to the custom label, it invokes the Doendtag method of the label processor, which returns a constant eval_page or skip_page to the Web container after the Doendtag method finishes executing. If the Doendtag method returns a constant Eval_page,web the container then executes the JSP code that follows the end tag in the JSP page, and if the Doendtag method returns the Skip_page,web container ignores everything that is behind the closing tag in the JSP page.

From the role of the doStartTag and Doendtag methods and the role of the return value can be seen, the development of custom tags can be in the doStartTag method and Doendtag method in the body to write the appropriate Java program code to achieve specific functions, By controlling the return value of the doStartTag method and the Doendtag method, you can also tell the Web container whether to execute the tag body content in the custom label and the content that is behind the end tag of the custom label in the JSP page.

2.3. Iterationtag interface

  The Iterationtag interface inherits the tag interface and adds a Doafterbody method and a Eval_body_again constant on the basis of the tag interface. The implementation of the Iterationtag interface label in addition to the ability to complete the tag interface can be done, but also to inform the Web container whether to repeat the tag body content. for custom labels that implement the Iterationtag interface, the Web container will invoke the label processor's Doafterbody method after it finishes executing the label body of the custom label, and the Doafterbody method can return a constant to the Web container eval_body_ Again or skip_body. If the Doafterbody method returns the Eval_body_again,web container, the contents of the tag body are repeated once, and then the Doafterbody method is called again after execution, until the Doafterbody method returns a constant skip_body , the Web container will begin processing the tag's closing tag and calling the Doendtag method.

As can be seen, when developing a custom label, it is possible to control the return value of the Doafterbody method to tell the Web container whether the tag body content is repeated, thus achieving the effect of looping the contents of the label body. For example, you can iterate through a label that implements the Iterationtag interface to output all the elements in a collection, specifying the output format of the element in the tag body section.

  The default implementation class TagSupport for the Iterationtag interface is also available in the JSP API, and we can inherit and extend the TagSupport class when writing a label processor class for a custom label. This simplifies the development effort compared to implementing the Iterationtag interface.

2.4. Bodytag interface

  The Bodytag interface inherits the Iterationtag interface and adds two methods (Setbodycontent, Doinitbody) and a eval_body_buffered constant on the basis of the Iterationtag interface. The implementation of the Bodytag interface label in addition to the completion of the Iterationtag interface can be done, you can also modify the contents of the label body. for custom labels that implement the Bodytag interface, the doStartTag method of the label processor can return not only the constants Eval_body_include or Skip_body explained previously, but also the constants eval_body_buffered. If the doStartTag method returns the Eval_body_buffered,web container, a Bodycontent object is created that is dedicated to capturing the result of the label body. The Setbodycontent method of the label processor is then called to pass a reference to the Bodycontent object to the label processor, which then writes the result of the tag body to the Bodycontent object. In the subsequent event method of the label processor, the result of the tag body can be obtained through a reference to the previously saved Bodycontent object. The Bodycontent object-specific method is then called to modify and control the output of the contents of the Bodycontent object, that is, the execution result of the label body.

  The implementation class Bodytagsupport for the Bodytag interface is also available in the JSP API, and we can inherit and extend the Bodytagsupport class when writing a label processor class that modifies the custom label content of the tag body. This simplifies the development effort compared to implementing the Bodytag interface.

2.5. Simpletag interface

  The Simpletag interface is a new label interface in JSP2.0 . Because the traditional label use three label interface to complete different functions, it seems too cumbersome, not conducive to the promotion of label technology, so sun company to reduce the learning difficulty of the label technology, in JSP 2.0 defined a more simple, easy to write and invoke the Simpletag interface. The biggest difference between the Simpletag interface and the traditional label interface is that the Simpletag interface only defines a Dotag method for handling the label logic that is called when the Web container executes a custom label and is called only once. Functions that are performed using the traditional label interface, such as whether to execute the tag body, iterate the label body, and modify the contents of the label body, can be done in the Dotag method.

  The Simpletag interface implementation Class Simpletagsupport is also available in the JSP API, and we can inherit and extend the Simpletagsupport class when writing a simple tag. This simplifies the development effort compared to implementing the Simpletag interface.

2.6. The return value description that can be returned by each method in the traditional label interface

The main methods in the tag interface, the Iterationtag interface and the Bodytag interface, and the description of the return values that they can return, are listed.

  

Third, the development of traditional tags to implement page logic

Developers often need to introduce some logic into the page when writing JSP pages, such as:

    • Controls whether a portion of a JSP page is executed.
    • Controls whether the entire JSP page executes.
    • Controls the repeated execution of JSP page content.
    • Modifies the JSP page content output.

In addition to removing the JSP page Java code, the custom label can also implement the above functions.

3.1, control the JSP page part of the content is executed

Write a class to implement the tag interface, control the return value of the doStartTag () method, if the method returns Eval_body_include, the tag body is executed, and if Skip_body is returned, the label body is not executed.

Sun provides a default implementation class for the tag interface for all methods that implement the tag interface in the Tagsupport,tagsupport class, so we can write a class that inherits the TagSupport class and then overrides the doStartTag method.

The sample code is as follows:

Tagdemo1.java

1 package Me.gacl.web.tag; 2  3 import javax.servlet.jsp.JspException; 4 import javax.servlet.jsp.tagext.Tag; 5 import Javax.servlet.jsp.tagext.TagSupport; 6  7/** 8  * @author gacl 9  * TagSupport class implements the tag interface, TagDemo1 inherits TagSupport class,  */12 public  class TagDemo1 extends TagSupport {*/     * Override the doStartTag method to control whether the tag body executes the      @see javax.servlet.jsp.tagext.tagsupport# doStartTag ()      */17     @Override18 public     int doStartTag () throws Jspexception {         ///If this method returns Eval_ Body_include, the label body is executed, and if the Skip_body is returned, the label body is not executed         //return tag.eval_body_include;21         return tag.skip_body;22     }23}

Add a description of the label processing class to the TLD file in the Web-inf directory, as follows:

1 <tag>2         <name>demo1</name>3         <tag-class>me.gacl.web.tag.tagdemo1</ Tag-class>4         <!--demo1 Tag has a label body, so here the body-content is set to Jsp-->5         <body-content>jsp</ Body-content>6 </tag>

Import and use the custom tags in the JSP page as follows:

1 <%@ page language= "java" pageencoding= "UTF-8"%> 2 <%--Import custom tag libraries in JSP pages--%> 3 <% @taglib uri= "/gacl" Prefi x= "GaCl"%> 4 <! DOCTYPE html> 5 

The results are as follows:

 

3.2. Control whether the entire JSP page executes

Write a class to implement the tag interface, control the return value of the Doendtag () method, if this method returns Eval_page, executes the remaining JSP page of the label, and if Skip_page is returned, the remaining jsps are not executed.

The sample code is as follows:

Tagdemo2.java

1 package Me.gacl.web.tag; 2  3 import javax.servlet.jsp.JspException; 4 import javax.servlet.jsp.tagext.Tag; 5 import Javax.servlet.jsp.tagext.TagSupport; 6  7/** 8  * @author gacl 9  * TagSupport class implements the tag interface, TagDemo2 inherits TagSupport class */11  Extends tagsupport{12     /* Override the Doendtag method to control whether the JSP page executes the      * @see Javax.servlet.jsp.tagext.tagsupport#doendtag ( )      */16     @Override17 public     int Doendtag () throws Jspexception {         ///If this method returns Eval_page, The remaining JSP page of the label is executed, and if Skip_page is returned, the remaining jsp19         return tag.skip_page;20         //return tag.eval_page;21     }22 23 is not executed     24}

Add a description of the label processing class to the TLD file in the Web-inf directory, as follows:

1 <tag>2         <name>demo2</name>3         <tag-class>me.gacl.web.tag.tagdemo2</ Tag-class>4         <!--demo2 Tag has no label body, so here the body-content is set to Empty-->5         <body-content>empty</ Body-content>6 </tag>

Import and use the custom tags in the JSP page as follows:

1 <%@ page language= "java" pageencoding= "UTF-8"%> 2 <%--Import custom tag libraries in JSP pages--%> 3 <% @taglib uri= "/gacl" Prefi x= "GaCl"%> 4 <! DOCTYPE html> 5 

The results are as follows:

 

3.3, control the JSP page content repeat execution

Write a class to implement the Iterationtag interface, control the return value of the Doafterbody () method, and if this method returns Eval_body_again, the Web server executes the label body once, and so on, Always executes until the Doafterbody method returns skip_body, the label body is not executed repeatedly.

The sample code is as follows:

Tagdemo3.java

1 package Me.gacl.web.tag; 2  3 import javax.servlet.jsp.JspException; 4 import javax.servlet.jsp.tagext.IterationTag; 5 import Javax.servlet.jsp.tagext.Tag; 6 Import Javax.servlet.jsp.tagext.TagSupport; 7  8 public class TagDemo3 extends TagSupport {9     int x = 5;11     @Override12 public     int doStartTag () throw S jspexception {return         tag.eval_body_include;14     }15     /* Control the return value of the Doafterbody () method,      * If this method returns Eval_body_again, then the Web server executes the label body once, and so on, and so on, until the      Doafterbody method returns skip_body, the label body will not be executed again.      * @see javax.servlet.jsp.tagext.tagsupport#doafterbody ()      */21 @Override22 public     int Doafterbody () throws jspexception {         x--;24         if (x>0) {             return iterationtag.eval_body_again;26         }else{27             return iterationtag.skip_body;28         }29     }30 31}

Add a description of the label processing class to the TLD file in the Web-inf directory, as follows:

1 <tag>2         <name>demo3</name>3         <tag-class>me.gacl.web.tag.tagdemo3</tag-class >4         <!--DEMO3 Tag has a label body, so here the body-content is set to Jsp-->5         <body-content>jsp</body-content>6 </tag>

Import and use the custom tags in the JSP page as follows:

1 <%@ page language= "java" pageencoding= "UTF-8"%> 2 <%--Import custom tag libraries in JSP pages--%> 3 <% @taglib uri= "/gacl" Prefi x= "GaCl"%> 4 <! DOCTYPE html> 5 

The results are as follows:

  

3.4. Modify JSP page content output

Writing a class to implement the Bodytag interface, the control doStartTag () method returns eval_body_buffered, the Web server creates a Bodycontent object capture tag body, and then in the Doendtag () method body, The Bodycontent object representing the label body is obtained, so that the label body can be modified.

Sun's Bodytag interface provides a default implementation class Bodytagsupport,bodytagsupport all the methods that implement the Bodytag interface, so we can write a class that inherits the Bodytagsupport class. Then rewrite the doStartTag method and the Doendtag () method as needed.

The sample code is as follows:

Tagdemo4.java

 1 package Me.gacl.web.tag; 2 3 Import java.io.IOException; 4 5 Import Javax.servlet.jsp.JspException; 6 Import javax.servlet.jsp.tagext.BodyContent; 7 Import Javax.servlet.jsp.tagext.BodyTag; 8 Import Javax.servlet.jsp.tagext.BodyTagSupport; 9 Import javax.servlet.jsp.tagext.tag;10/**12 * @author GACL13 * Bodytagsupport class implements Bodytag interface interface, TagDemo4 inheritance Bodytagsup Port class */15 public class TagDemo4 extends Bodytagsupport {16 17/* Control doStartTag () method returns EVAL_BODY_BUFFERED18 * @se e Javax.servlet.jsp.tagext.bodytagsupport#dostarttag () */20 @Override21 public int doStartTag () throws Jspe xception {return bodytag.eval_body_buffered;23}24 @Override26 public int Doendtag () throws jspexception {//this.getbodycontent () Gets the Bodycontent object representing the label body bodycontent bodycontent = this. Getbodycontent (); 30//Get tag body to String content = Bodycontent.getstring (); 32//Modify the contents of the tag body to convert the contents of the label body into large Write a String ResUlt = content.touppercase (); try {35//Output modified content This.pageContext.getOut (). Write (Result)  ; PNs} catch (IOException e) {new runtimeexception (e);}40 Tag.eval_page;42}43}

Add a description of the label processing class to the TLD file in the Web-inf directory, as follows:

1 <tag>2         <name>demo4</name>3         <tag-class>me.gacl.web.tag.tagdemo4</tag-class >4         <!--Demo4 Tag has a label body, so here the body-content is set to Jsp-->5         <body-content>jsp</body-content>6 </tag>

Import and use the custom tags in the JSP page as follows:

1 <%@ page language= "java" pageencoding= "UTF-8"%> 2 <%--Import custom tag libraries in JSP pages--%> 3 <% @taglib uri= "/gacl" Prefi x= "GaCl"%> 4 <! DOCTYPE html> 5 

The results are as follows:

  

Iv. Summary of JSP traditional label development

In the current JSP tag development, very few directly use the traditional label to develop, the current use more is a simple label, so the traditional JSP tag development to understand, the next focus on the development of the JSP simple tag

Javaweb Learning Summary (24)--jsp Traditional label development

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.