Javaweb Learning Summary (26)--jsp simple label Tag Library Development (II.)

Source: Internet
Author: User
Tags tag name tld


Javaweb Learning Summary (26)--jsp simple label Tag Library Development (II.) introduction of Jspfragment class


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 :


    1. Write setter methods for each property in the label processor
    2. 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 * The SimpleTagSupport class implements the SimpleTag interface,
10 * SampleTagDemo5 class inherits SimpleTagSupport
11 * Control the number of executions of the label body through the attributes of the label
12 * /
13 public class SimpleTagDemo5 extends SimpleTagSupport {
14
15 / **
16 * Define the attributes of the label
17 * /
18 private int count;
19
20 / ** count property corresponding set method
21 * @param count
twenty two      */
23 public void setCount (int count) {
24 this.count = count;
25}
26
27 / * Simple tags can use this method to complete all business logic
28 * @see javax.servlet.jsp.tagext.SimpleTagSupport # doTag ()
29 * Override the doTag method to control the number of executions of the tag body through the attributes of the tag
30 * /
31 @Override
32 public void doTag () throws JspException, IOException {
33 for (int i = 0; i <count; i ++) {
34 this.getJspBody (). Invoke (null);
35}
36}
37
38}


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



1 <tag>
  2 <!-Tag name->
  3 <name> demo5 </ name>
  4 <!-Tag handler class->
  5 <tag-class> me.gacl.web.simpletag.SimpleTagDemo5 </ tag-class>
  6 <!-What the tag body allows->
  7 <body-content> scriptless </ body-content>
  8         
  9 <!-Tag attribute description->
10 <attribute>
11 <description> count attribute of description tag </ description>
12 <!-Tag's count attribute->
13 <name> count </ name>
14 <required> true </ required>
15 <!-Rtexprvalue is used to indicate whether the attribute value of the label can be an expression,
16 Generally set to true, true means that the attribute value of the tag 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 a custom tag library in a jsp page-%>
  3 <%-<% @ taglib uri = "/ simpletag" prefix = "gacl"%>-%>
  4 <%-You can also use this method to import the tag library in the jsp page, and set the uri directly to the directory where the tld file of the tag library is located.
  5 <% @ taglib uri = "/ WEB-INF / simpletag.tld" prefix = "gacl"%>
  6 <! DOCTYPE HTML>
  7 <html>
  8 <head>
  9 <title> Control the number of executions of the tag body through the attributes of the tag </ title>
10 </ head>
11
12 <body>
13 <%-Use custom tags in jsp pages, tags have a count attribute-%>
14 <gacl: demo5 count = "2">
15 <h1> Lonely Troll Wolf </ h1>
16 </ gacl: demo5>
17 </ body>
18 </ html>


If the attribute value of the tag is 8 basic data types, the JSP engine will automatically convert to the corresponding type when the string is passed in the JSP page, but if the attribute value of the tag is a composite data type, the JSP engine cannot automatically convert of

Example 2: The attribute value received by the tag is a composite data type. How to assign a value to the attribute of the tag

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 javax.servlet.jsp.tagext.SimpleTagSupport;
 8 
 9 /**
10 * @author gacl
11 * The SimpleTagSupport class implements the SimpleTag interface,
12 * SampleTagDemo6 class inherits SimpleTagSupport
13 * Label attribute description
14 * /
15 public class SimpleTagDemo6 extends SimpleTagSupport {
16
17 / **
18 * Define the attributes of the label
19 * /
20 private Date date;
twenty one     
22 / ** set method corresponding to the date attribute
23 * @param date
twenty four      */
25 public void setDate (Date date) {
26 this.date = date;
27}
28
29 / * Simple tags use this method to complete all business logic
30 * @see javax.servlet.jsp.tagext.SimpleTagSupport # doTag ()
31 * Override the doTag method and output the date attribute value
32 * /
33 @Override
34 public void doTag () throws JspException, IOException {
35 this.getJspContext (). GetOut (). Write (date.toLocaleString ());
36}
37}

Add a description of the tag in the tld file under the WEB-INF directory as follows:

 1 <tag>
 2 <!-Tag name->
 3 <name> demo6 </ name>
 4 <!-Tag handler class->
 5 <tag-class> me.gacl.web.simpletag.SimpleTagDemo6 </ tag-class>
 6 <!-What the tag body allows->
 7 <body-content> empty </ body-content>
 8         
 9 <!-Tag attribute description->
10 <attribute>
11 <description> date attribute of the description tag </ description>
12 <!-Tag's date attribute, composite data type->
13 <name> date </ name>
14 <required> true </ required>
15 <!-Rtexprvalue is used to indicate whether the attribute value of the label can be an expression,
16 Generally set to true, true means that the attribute value of the tag can be an expression->
17 <rtexprvalue> true </ rtexprvalue>
18 </ attribute>
19 </ tag>

 Introduce tag library in jsp page and use custom tags

  
1 <% @ page language = "java" import = "java.util. *" PageEncoding = "UTF-8"%>
 2 <%-Import a custom tag library in a jsp page-%>
 3 <%-<% @ taglib uri = "/ simpletag" prefix = "gacl"%>-%>
 4 <%-You can also use this method to import the tag library in the jsp page, and set the uri directly to the directory where the tld file of the tag library is located.
 5 <% @ taglib uri = "/ WEB-INF / simpletag.tld" prefix = "gacl"%>
 6 <! DOCTYPE HTML>
 7 <html>
 8 <head>
 9 <title> If the attribute value received by the tag is a composite data type, how to assign a value to the attribute of the tag </ title>
10 </ head>
11
12 <body>
13 <%-
14 Use custom tags in jsp pages, tags have a date attribute, which is a composite data type
15 If the attribute value of the tag is 8 basic data types, when the JSP page passes a string, the JSP engine will automatically convert to the corresponding type
16 But if the attribute value of the tag is a composite data type, the JSP engine cannot automatically convert it.
17 If you assign a string to the date attribute of the demo6 tag, the following error will occur when running the tag:
18 Unable to convert string "1988-05-07" to class "java.util.Date" for attribute "date":
19 Property Editor not registered with the PropertyEditorManager
20 <gacl: demo6 date = "1988-05-07">
21 </ gacl: demo6>
22-%>
23 <%-If you must assign a value to a compound attribute of a label, you can use an expression to assign a value to the compound attribute, as follows:-%>
twenty four       
25 <%
26 Date d = new Date ();
27 request.setAttribute ("date", d);
28%>
29
30 <gacl: demo6 date = "$ {date}" />
31 <hr />
32 <gacl: demo6 date = "<% = new Date ()%>" />
33 </ body>
34 </ html>

The operation effect is as follows:



2.1, <attribute> element description in tld file for tag attributes
<The <attribute> child element of the <tag> element is used to describe an attribute of a custom tag. Each attribute of a custom tag must correspond to an <attribute> element.

E.g:

 1 <tag>
 2 <!-Tag name->
 3 <name> demo5 </ name>
 4 <!-Tag handler class->
 5 <tag-class> me.gacl.web.simpletag.SimpleTagDemo5 </ tag-class>
 6 <!-What the tag body allows->
 7 <body-content> scriptless </ body-content>
 8         
 9 <!-Tag attribute description->
10 <attribute>
11 <description> count attribute of description tag </ description>
12 <!-Tag's count attribute->
13 <name> count </ name>
14 <required> true </ required>
15 <!-Rtexprvalue is used to indicate whether the attribute value of the label can be an expression,
16 Generally set to true, true means that the attribute value of the tag can be an expression->
17 <rtexprvalue> true </ rtexprvalue>
18 </ attribute>
19 </ tag>

Description of the child elements of the <attribute> element:



At this point, even if the development of simple labeling technology is all done, in the next blog post, some custom labeling cases will be written to deepen the learning and understanding of custom labeling technology.

Javaweb learning summary (26)-jsp simple tag tag library development (2)


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.