First, Jspfragment class introduction
The Javax.servlet.jsp.tagext.JspFragment class is defined in JSP2.0, whose instance object represents a JSP fragment in a JSP page that conforms to the JSP syntax specification, which cannot contain JSP script elements.
when the Web container handles the label body of a simple label, the label body content is represented by a Jspfragment object and the Setjspbody method of the tag processor object is called to pass the Jspfragment object to the label processor object. only two methods are defined in the Jspfragment class, as follows:
Getjspcontext method
Used to return a Jspcontext object representing the calling page. public abstract void Invoke (Java.io.Writer out)
Used to execute the JSP code fragment represented by the Jspfragment object, which specifies the output stream object to which the execution result of the Jspfragment object is written, if the value passed to the parameter out is NULL, The execution result is written to the output stream object returned by the Jspcontext.getout () method. (In short, it can be understood as writing to a browser)
1.1, the Invoke method detailed
The Jspfragment.invoke method is the most important method of jspfragment, which can be used to control whether the content of the tag body is executed and output, whether the content of the tag body is iterated, or the result of the label body execution is modified before output. For example:
If the Jspfragment.invoke method is not called in the label processor, the result is equivalent to ignoring the contents of the tag body;
Repeated calls to the Jspfragment.invoke method in the label processor, the label body content will be repeated execution;
To modify the contents of a tag in a label processor, simply specify an output stream object (such as StringWriter) that extracts the result data when the Invoke method is called, so that the execution result of the tag body is output to the output stream object. Then, from the output stream object to remove the data to modify and then output to the target device, you can modify the label body purpose.
Ii. development of labels with attributes
Custom labels can define one or more properties so that when you apply a custom label to a JSP page, you can set the values of these properties, passing parameter information for the label processor, which improves the flexibility and reusability of the label.
To have a custom label with attributes, you typically need to complete two tasks :
- Write setter methods for each property in the label processor
- Attributes of the description tag in the TLD file
When defining a property for a custom tag, each property must be named by the property name of JavaBean, which defines the setter method for the property names in the label processor to receive the property values passed in when the JSP page invokes the custom label. For example, a property URL, the corresponding seturl (String URL) method is defined in the label processor class. After the corresponding set method is defined in the label processor, the JSP engine invokes the Set property method to set properties for the label before parsing the start tag, which is called the doStartTag method.
2.1. Developing tag examples with attributes
Example 1: Controlling the number of executions of the label body through the label's properties
The sample code is as follows:
Simpletagdemo5.java
1 package Me.gacl.web.simpletag; 2 3 import java.io.IOException; 4 import javax.servlet.jsp.JspException; 5 import Javax.servlet.jsp.tagext.SimpleTagSupport; 6 7/** 8 * @author gacl 9 * Simpletagsupport class implements the Simpletag interface, the SampleTagDemo5 class inherits SimpleTagSupport11 * Control the number of execution times of the tag body via the label's properties */13 public class SimpleTagDemo5 extends Simpletagsupport { /**16 * Define label Properties */18 private int count;19 /**count Property corresponding Set method * @param count22 */23 Public void SetCount (int count) { This.count = count;25 }26/ * Simple tags Use this method to complete all business logic. * @see Javax.servlet.jsp.tagext.simpletagsupport#dotag () * Rewrite the Dotag method, Control the number of execution times of the tag body through the properties of the label */31 @Override32 public void Dotag () throws Jspexception, IOException { for (int i = 0; i < count; i++) { this.getjspbody (). Invoke (null); }36 }37 38}
Add a description of the label to the TLD file in the Web-inf directory, as follows:
1 <tag> 2 <!--label name--3 <name>demo5</name> 4 <!--label Processor class--5 < Tag-class>me.gacl.web.simpletag.simpletagdemo5</tag-class> 6 <!--label body allowed Content--7 < Body-content>scriptless</body-content> 8 9 <!--Label Property Description-->10 <attribute>11 <description> describes the Count property of a label </description>12 <!--the tag's Count property-->13 <name>count </name>14 <required>true</required>15 <!--Rtexprvalue is used to indicate whether a label's property value can be an expression, 16 A general setting of true,true means that a property value that allows a label can be an expression-->17 <rtexprvalue>true</rtexprvalue>18 </ Attribute>19 </tag>
Introducing tag libraries on JSP pages and using custom tags
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:
If the tag's attribute value is 8 basic data types, the JSP engine will automatically convert to the appropriate type when the JSP page passes the string, but if the label's property value is a composite data type, then the JSP engine cannot be automatically converted
Example 2: A tag receives a property value that is a composite data type, how to assign a value to a label's property
The sample code is as follows:
Simpletagdemo6.java
1 package Me.gacl.web.simpletag; 2 3 import java.io.IOException; 4 import java.util.Date; 5 6 import javax.servlet.jsp.JspException; 7 import Java X.servlet.jsp.tagext.simpletagsupport; 8 9/**10 * @author gacl11 * Simpletagsupport class implements the Simpletag interface, SampleTagDemo6 class Inherits SimpleTagSupport13 * Label Property Description */15 Public class SimpleTagDemo6 extends Simpletagsupport {16 /**18 * Define the properties of the label */20 private Date date;21 The set method corresponding to the/**date property is @param Date24 */25 public void setDate (date date) { this.date = date;27 }28/ * Simple tags Use this method to complete all the business logic. * @see Javax.servlet.jsp.tagext.simpletagsupport#dotag () * Rewrite the Dotag method, Output Date Property value */33 @Override34 public void Dotag () throws Jspexception, IOException { This.getjspcontext (). Getout (). Write (date.tolocalestring ()); }37}
Add a description of the label to the TLD file in the Web-inf directory, as follows:
1 <tag> 2 <!--label name--3 <name>demo6</name> 4 <!--label Processor class--5 < Tag-class>me.gacl.web.simpletag.simpletagdemo6</tag-class> 6 <!--label body allowed Content--7 < Body-content>empty</body-content> 8 9 <!--Label Property Description-->10 <attribute>11 < Description> describes the date property of the label </description>12 <!--The Date property of the label, the composite data type-->13 <name>date </name>14 <required>true</required>15 <!--Rtexprvalue is used to indicate whether a label's property value can be an expression, 16 A general setting of true,true means that a property value that allows a label can be an expression-->17 <rtexprvalue>true</rtexprvalue>18 </ Attribute>19 </tag>
Introducing tag libraries on JSP pages and using custom tags
1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <%--Import custom tag libraries in JSP pages--%> 3 <%--& lt;% @taglib uri= "/simpletag" prefix= "GaCl"%>--%> 4 <%--in JSP pages you can also import the tag library in this way, directly set the URI to the directory where the TLD file of the tag library--% > 5 <% @taglib uri= "/web-inf/simpletag.tld" prefix= "GaCl"%> 6 <! DOCTYPE html> 7 The results are as follows:
2.1. Description of <attribute> elements used to describe tag attributes in TLD files The <attribute> child element of the <tag> element is used to describe a property of the custom label, and each property of the custom label has a <attribute> element
For example:
1 <tag> 2 <!--label name--3 <name>demo5</name> 4 <!--label Processor class--5 < Tag-class>me.gacl.web.simpletag.simpletagdemo5</tag-class> 6 <!--label body allowed Content--7 < Body-content>scriptless</body-content> 8 9 <!--Label Property Description-->10 <attribute>11 <description> describes the Count property of a label </description>12 <!--the tag's Count property-->13 <name>count </name>14 <required>true</required>15 <!--Rtexprvalue is used to indicate whether a label's property value can be an expression, 16 A general setting of true,true means that a property value that allows a label can be an expression-->17 <rtexprvalue>true</rtexprvalue>18 </ Attribute>19 </tag>
Description of the child elements of the <attribute> element:
In this case, the development of simple label technology even if it is all finished, in the next blog post will be written some custom label cases to deepen the learning and understanding of custom labeling technology.
Javaweb Learning Summary (26)--jsp simple label Tag Library Development (II.)