Javaweb Learning Summary (25)--jsp Simple label Development (i)

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

Javaweb Learning Summary (25)--jsp Simple label Development (i) one, simple label (SIMPLETAG)

As the traditional label using three label interface to complete different functions, appear too cumbersome, not conducive to the promotion of label technology, 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 to implement the function of the label.

  

the label that implements the Simpletag interface is often referred to as a simple label. A simple Label defines 5 methods:

    • Setjspcontext method
    • SetParent and GetParent methods
    • Setjspbody method
    • Dotag method (very important), simple label Use this method to complete all the business logic
Ii. Simpletag Method Introduction 2.1, Setjspcontext method

Used to pass the PageContext object of a JSP page to the label processor object

2.2. SetParent method

Used to pass the parent tag processor object to the current label processor object

2.3. GetParent method

The parent tag processor object used to get the current label

2.4. Setjspbody method

Used to pass the Jspfragment object representing the label body to the label processor object

2.5. Dotag method

Used to complete all of the label logic, including output, iteration, modify tag body contents, etc. The javax.servlet.jsp.SkipPageException exception can be thrown in the Dotag method to inform the Web container that it is no longer executing the contents of the JSP page after the closing tag, which is equivalent to returning Tag.skip in the traditional label's Doendtag method. The case of _page constants.

Third, the execution order of the Simpletag interface method

When the Web container starts executing the label, the following method is called to complete the initialization of the tag:

    1. The Web container invokes the Setjspcontext method of the label processor object, passing the PageContext object representing the JSP page to the label processor object.
    2. The Web container invokes the SetParent method of the label processor object, passing the parent tag processor object to the label processor object. Note that the Web container calls this method only if the label has a parent tag.
    3. If a property is set when the tag is called, the container invokes the setter method for each property to pass the property value to the label processor object. If the property value of a label is an El expression or a script expression, the Web container evaluates the expression first and then passes the value to the label processor object.
    4. If a simple tag has a tag body, the Web container calls the Setjspbody method to pass in the Jspfragment object that represents the label body.
    5. When the tag is executed, the Web container invokes the Dotag () method of the label processor, which enables the developer to execute, iterate, and modify the label body by manipulating the Jspfragment object in the method body.
Iv. development of simple tags to implement page logic

Sun has provided a default implementation class Simpletagsupport,simpletagsupport all methods for implementing the Simpletag interface for the Simpletag interface. So we can write a class that inherits the Simpletagsupport class and then rewrite the Dotag method based on the business need.

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

Write a class to inherit Simpletagsupport, and then rewrite the Dotag method without calling the Jspframent.invoke method inside the Dotag method.

The sample code is as follows:

Simpletagdemo1.java

 1 package Me.gacl.web.simpletag; 2 3 Import java.io.IOException; 4 5 Import Javax.servlet.jsp.JspException; 6 Import Javax.servlet.jsp.PageContext; 7 Import javax.servlet.jsp.tagext.JspFragment; 8 Import Javax.servlet.jsp.tagext.SimpleTagSupport; 9/**11 * @author GACL12 * Simpletagsupport class implements the Simpletag interface, the SampleTagDemo1 class inherits SimpleTagSupport14 */15 public CLA SS SimpleTagDemo1 extends Simpletagsupport {16 17 */* Simple label Use this method to complete all business logic * @see Javax.servlet.jsp.tagext.Sim Pletagsupport#dotag () 19 * Override the Dotag method to control whether the label body executes the */21 @Override22 public void Dotag () throws Jspexception         , IOException {23//get JspFragment24 jspfragment jspfragment = This.getjspbody () representing the JSP tag body; 25 26 Get JSP page of the PageContext object//pagecontext PageContext = (PageContext) jspfragment.getjspcontext (); 28/         /Call JspWriter to output the contents of the tag body to the browser//jspfragment.invoke (Pagecontext.getout ()); 30 31//Output the contents of the label body to browser 32 Jspfragment.iNvoke (null); 33 34}35} 

Create a new Simpletag.tld file in the Web-inf directory, and then add a description of the label processing class to the Simpletag.tld file as follows:

  

The Simpletag.tld file code is as follows:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 3 <taglib xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" 4 xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "5 xsi:schemalocation=" HTTP://JAVA.SUN.COM/XML/NS/J2EE/http Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "6 version=" 2.0 "> 7 <!--description To add a description of the Taglib (tag library)- 8 <description> aloof Wolf developed Simpletag custom Tag library </description> 9 <!--taglib (tag Library) version number-->10 &LT;TL         Ib-version>1.0</tlib-version>11 <short-name>gaclsimpletaglibrary</short-name>12 <!--13 Set a Uri,uri to/from the custom tag library, and/or the following, as/simpletag here, 14 when referencing a tag library in a JSP page, it is necessary to find the tag library by Uri 15 in the JSP page to introduce the tag library: &L t;% @taglib uri= "/simpletag" prefix= "GaCl"%>16-->17 <uri>/simpletag</uri>18 <!--a Taglib (Tag library) contains a number of custom labels, each of which uses a tag tag to describe-->20 <!--a tag tag corresponding to a custom label-->21 <tag>22 < Description>simpletag (Simple label) demo1</description>23 <!--24 Label processor class with a label name, when using a tag in a JSP page, the label name is used to find the label processor class to call 25 through Demo1 can find to the corresponding Me.gacl.web.simpletag.SimpleTagDemo1 class-->27 <name>demo1</name>28 <!--tag pair         Processor class-->29 <tag-class>me.gacl.web.simpletag.simpletagdemo1</tag-class>30 <!--31 There are four types of label bodies in the TLD file: Empty JSP scriptless Tagdepentend 32 The value of the label body Body-content in the Simple label (SAMPLETAG) is only allowed to be empty and SCRIPTL ESS, not allowed to be set to JSP, if set to JSP will appear exception 33 in the traditional label label body body-content value only allowed is empty and JSP34 if the label body body-content value set to TAGDEP Entend, it means that the contents of the tag body are used for the label processor class, 35 For example: Develop a query user's SQL tag, the label weight of the SQL statement is to the SQL label label processor to use the <gacl:sql >select * from user</gacl:sql>37 in this case, the SQL label <body-content> will be set to tagdepentend,tagdepentend use less , learn about-->39 <body-content>scriptless</body-content>40 </tag>41 </taglib&gt ;

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= "/simpletag" prefix= "GaCl"%> 4 <! DOCTYPE html> 5 

The results are as follows:

  

4.2, control the JSP page content repeat execution

Write a class to inherit Simpletagsupport, and then rewrite the Dotag method, and repeat the Jspframent.invoke method inside the Dotag method.

The sample code is as follows:

Simpletagdemo2.java

1 package Me.gacl.web.simpletag; 2  3 import java.io.IOException; 4  5 import javax.servlet.jsp.JspException; 6 import Javax.servlet.jsp.tagext.JspFragment; 7 Import Javax.servlet.jsp.tagext.SimpleTagSupport; 8  9/**10  * @author gacl11  * Simpletagsupport class implements the Simpletag interface,  SampleTagDemo2 class inheritance SimpleTagSupport13  */14 public class SimpleTagDemo2 extends Simpletagsupport {+/     * Simple label Use this method to complete all business logic      * @see javax.servlet.jsp.tagext.simpletagsupport#dotag ()      * Rewrite the Dotag method, Control label executed 5 times      */20     @Override21 public     void Dotag () throws Jspexception, IOException {         // Get JspFragment23         jspfragment jspfragment = this.getjspbody (         int i = 0; i < 5; i++)             representing the JSP tag body Output the contents of the label body to browser             jspfragment.invoke (null);         }28     }29}

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

1 <tag>2         <!--tag name-->3         <name>demo2</name>4         <!--tag Processor class-->5         < Tag-class>me.gacl.web.simpletag.simpletagdemo2</tag-class>6         <!--What the tag body allows, Scriptless indicates that the contents of the tag body are not allowed to be java script code-->7         <body-content>scriptless</body-content>8 </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= "/simpletag" prefix= "GaCl"%> 4 <! DOCTYPE html> 5 

The results are as follows:

  

4.3. Modify JSP page content output

Write a class to inherit Simpletagsupport, and then override the Dotag method, when the Dotag method calls the Jspframent.invoke method, let the execution result write a custom buffer, then the developer can take out the buffered data modification output.

The sample code is as follows:

Simpletagdemo3.java

 1 package Me.gacl.web.simpletag; 2 3 Import java.io.IOException; 4 Import Java.io.StringWriter; 5 6 Import Javax.servlet.jsp.JspException; 7 Import Javax.servlet.jsp.PageContext; 8 Import javax.servlet.jsp.tagext.JspFragment; 9 Import javax.servlet.jsp.tagext.simpletagsupport;10/**12 * @author GACL13 * Simpletagsupport class implements Simpletag interface, 14 * SampleTagDemo3 class inheritance SimpleTagSupport15 */16 public class SimpleTagDemo3 extends Simpletagsupport {17 18/* Simple label Use this method to      Complete all business logic * @see Javax.servlet.jsp.tagext.simpletagsupport#dotag () 20 * Rewrite the Dotag method, modify the contents of the tag body, convert the contents of the label body to uppercase 21         */22 @Override23 public void Dotag () throws Jspexception, IOException {24//Get JSPFRAGMENT25 representing the JSP tag body Jspfragment jspfragment = This.getjspbody (); StringWriter sw = new StringWriter (); 27//write the contents of the label body to S         W Stream Jspfragment.invoke (SW); 29//Gets the contents of the SW stream buffer (String content = Sw.getbuffer (). toString (); 31 Content = Content.touppercase (); 32        PageContext PageContext = (PageContext) this.getjspcontext (); 33//Output the modified content to the browser Pagecontex T.getout (). write (content); 35}36}

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

1 <tag>2         <!--tag name-->3         <name>demo3</name>4         <!--tag Processor class-->5         < Tag-class>me.gacl.web.simpletag.simpletagdemo3</tag-class>6         <!--What the tag body allows, Scriptless indicates that the contents of the tag body are not allowed to be java script code-->7         <body-content>scriptless</body-content>8 </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= "/ Simpletag "prefix=" GaCl "%>--%> 4 <%--in the JSP page can also be used to import the tag library, directly set the URI to the tag library's TLD file directory--%> 5 <% @taglib URI = "/web-inf/simpletag.tld" prefix= "GaCl"%> 6 <! DOCTYPE html> 7 

The results are as follows:

  

4.4. Control whether the entire JSP page executes

Write a class to inherit Simpletagsupport, and then rewrite the Dotag method, the Dotag method throws the Skippageexception exception, the JSP receives this exception, will ignore the label remaining JSP page execution.

The sample code is as follows:

Simpletagdemo4.java

1 package Me.gacl.web.simpletag; 2  3 import java.io.IOException; 4 import javax.servlet.jsp.JspException; 5 import Javax.servlet.jsp.SkipPageException; 6 Import Javax.servlet.jsp.tagext.SimpleTagSupport; 7  8/** 9  * @author GACL10  * Simpletagsupport class implements the Simpletag interface, one by one  * SampleTagDemo4 class inheritance SimpleTagSupport12  */13 public class SimpleTagDemo4 extends Simpletagsupport {+/     * Simple label Use this method to complete all business logic      * @see javax.servlet.jsp.tagext.simpletagsupport#dotag ()      * Override Dotag method, Control label the remaining JSP does not perform the      */19     @Override20 public     void Dotag () throws Jspexception, IOException {         // Throw a skippageexception exception to control the label the remaining JSP does not perform a         new Skippageexception ();     }24}

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

1 <tag>2         <!--tag name-->3         <name>demo4</name>4         <!--tag Processor class-->5         < Tag-class>me.gacl.web.simpletag.simpletagdemo4</tag-class>6         <!--label body allowed content, empty indicates that the label does not have a label body-- 7         <body-content>empty</body-content>8 </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= "/ Simpletag "prefix=" GaCl "%>--%> 4 <%--in the JSP page can also be used to import the tag library, directly set the URI to the tag library's TLD file directory--%> 5 <% @taglib URI = "/web-inf/simpletag.tld" prefix= "GaCl"%> 6 <! DOCTYPE html> 7 

The results are as follows:

  

Five, simple label development of some attention to detail 5.1, label class writing details

When developing the label class, do not go directly to implement the Simpletag interface, but should inherit the Simpletagsupport class, Simpletagsupport class is a default implementation class of the Simpletag interface, by inheriting the Simpletagsupport class, You can directly use the methods that the Simpletagsupport class has implemented, and if the method implementations of the Simpletagsupport class do not meet the business requirements, then the corresponding method can be rewritten according to the specific business situation.

5.2. Tag body type setting details in TLD file

Once we have developed a simple tag, we need to add a description of the tag to the TLD file, for example:

1 <tag>2         <!--tag name-->3         <name>demo2</name>4         <!--tag Processor class-->5         < Tag-class>me.gacl.web.simpletag.simpletagdemo2</tag-class>6         <!--What the tag body allows, Scriptless indicates that the contents of the tag body are not allowed to be java script code-->7         <body-content>scriptless</body-content>8 </tag>

After developing the good one tag, use <tag> in the TLD file to describe a label that includes the label name (name), the Label Processor Class (Tag-class), and the contents of the tag body (body-content).

There are four types of label bodies (Body-content) in the TLD file:empty, JSP, scriptless, Tagdependent

Simple Label tag body details Note the problem:
in the Simple label (Sampletag), the value of the label body Body-content is only allowed to be empty, scriptless, tagdependent, not allowed to be set to JSP, if set to JSP will appear exception :

1 The TLD for the class me.gacl.web.simpletag.SimpleTagDemo1 specifies an invalid body-content (JSP) for a Simpletag

  If the value of Body-content is set to empty, then it means that the label does not have a label body , and if it is set to scriptless, then it indicates that the label is labeled. However, the contents of the tag body can not be <%java code%>, for example:

1 <gacl:xdpdemo1>2    <%3            //nested in tag body Java code 4         int i= 0;5     %>6       aloof pale Wolf 7 </gacl: Xdpdemo1>

Otherwise, the following error will appear when you run the label:

1 Scripting elements (&lt;%!, &lt;jsp:declaration, &lt;%=, &lt;jsp:expression, &lt;%, &lt;jsp: Scriptlet) is disallowed here

The purpose of JSP tag is to remove the Java code written on the JSP page, if Java code is allowed in the JSP tag, then it violates the original intention of JSP tag technology design. Therefore, Java code is not allowed in the tag body of a simple tag.

The details of the traditional label tag body Note the problem:
The value of the label body Body-content in the traditional label is allowed to be empty, JSP, Scriptless, tagdependent, if thevalue of Body-content is set to JSP, then the label is labeled and the contents of the tag body can be arbitrary, including Java code , if it is set to scriptless, then the label is labeled Body, but the contents of the tag body cannot be Java code

If the value of the label body body-content of the traditional label and the simple label is set to tagdependent, then it means that the contents of the label body are used for the label processor class, and the tagdependent is used relatively little. See for yourself

5.3. The URI setting details of the tag library in the TLD file

If you are using or developing multiple tag libraries in one project, for example:

  

Then the tag library URI can not be set to the same, otherwise in the JSP page through the URI reference Tag library is not known which tag library, if it is really so clever, two tag library URI is exactly the same, as shown in:

  

Then when referring to the tag library in the JSP page, if "<% @taglib uri="/gacl"prefix=" GaCl "%>" is referenced, It is not possible to determine whether the currently referenced tag library is the label in the GACL.TLD tag library or the label in the SIMPLETAG.TLD tag library, because the URI of the two tag libraries is exactly "/gacl", in the same case as the reference URI of the two tag libraries, In order to be able to distinguish in JSP which tag library is referenced, you can change the reference way:<% @taglib uri= "The TLD file directory of the tag library to be referenced" prefix= "GaCl"%>, when introduced into the tag library using taglib directives , the URI attribute of the taglib directive is specified as the TLD file directory of the tag library, so that it can be distinguished, for example:

Reference GACL.TLD Tag library: <% @taglib uri= "/web-inf/gacl.tld" prefix= "GaCl"%>,

Reference SIMPLETAG.TLD Tag library: <% @taglib uri= "/web-inf/simpletag.tld" prefix= "GaCl"%>

So when multiple tag libraries are referenced in the project, if the tag library URI is exactly the same, it can be solved in this way.

Vi. Summary of Simple label development steps

1, write a class to inherit the Simpletagsupport class, and then according to the business needs to rewrite the Simpletagsupport class has implemented methods, generally only need to rewrite the Dotag () method.

2. Create a TLD file in the Web-inf directory and add a description of the tag to the TLD file. TLD files may not be placed in the Web-inf directory or in other directories, and are used in the Web-inf directory.

Javaweb Learning Summary (25)--jsp Simple label Development (i)

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.