I. Introduction of Labels
In fact, we have used a few tags in the JSP, such as <jsp:forward>, <jsp:include>, these tags are predefined, if we need to customize the label, we need to learn the label programming.
The advantage of label programming is flexibility + encapsulation.
Flexibility is reflected in the assignment of attributes, which can be assigned arbitrary values. For example <jsp:forward page= "A" >//a can be assigned any URL;
Encapsulation manifests itself in the encapsulation of internal behavior, because the label is implemented with a class, so the class method can contain arbitrary complex actions.
So the tag is to be able to use as few scriptlet in the JSP as possible;
Like what:
<table>
<% for
(int i=0;i<10;i++) {
%>
<tr>
<% for
(int j=0;j<10 ; j + + {
%>
<td><%=i*j%></td>
<%
}
%>
</tr>
<%
}
%>
</table>
This code is very confusing, but if you wrap the code in a label from the tag, the code is much clearer.
Next is I through the tag package after the JSP page effect;
<%@ page contenttype= "text/html" pageencoding= "GBK"%> <%@ taglib prefix=
"Xiazdong" uri= "Xiazdong"%>
Is it greatly shortened?? Let's see what I've done behind my back.
Tabletagsupport.java
Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
public class Tabletagsupport extends tagsupport{
private String row;
Private String col;
Public String GetRow () {return
row;
}
Public String Getcol () {return
col;
}
public void Setrow (String row) {
this.row = row;
}
public void Setcol (String col) {
this.col = col;
}
public int doStartTag () throws jspexception{
JspWriter out = Super.pageContext.getOut ();
try{
out.println ("<table border=\" 3\ ">");
for (int i=0;i<integer.parseint (row); i++) {
out.println ("<tr>");
for (int j=0;j<integer.parseint (col); j + +) {
out.println ("<td>" +i*j+ "</td>");
Out.println ("</tr>");
}
Out.println ("</table>");
}
catch (Exception e) {} return
tagsupport.skip_body
}}
Is it amazing ... Then let's look at the implementation process.
Note that the Jsp-api.jar file in the Tomcat\lib must be configured in Classpath before the label is written;
Second, the basic label preparation
1, TagSupport class
If you want to write a label class, you must inherit javax.servlet.jsp.tagext.TagSupport;
Many common methods are available in TagSupport:
(1) public int doStartTag() throws jspexception; Called when the label starts
Ability to return Skip_body (skip tag body), eval_body_include (Execute tag body)
(2) public int Doendtag() throws jspexception; Called when the label ends
Able to return to Skip_page (immediately stop execution), eval_page (JSP normal operation completed);
(3) int skip_body; Skip Label Body
(4) int eval_body_include; Execution label Body
(5) int Eval_body_again; Repeated execution of the label body, mainly because of the collection iteration output, can only be used in doafterbody;
(6) public int Doafterbody() throws jspexception; The function that is called after the execution of the label body;
Ability to return Skip_body (end tag body), Eval_body_again (Repeat execution label body)
(7) JspWriter out = Super.pageContext.getOut ();//Get output stream to the Web page;
For example, these functions and constants represent what they mean:
<xiazdong:hello> <!--label head-->
The execution process is as follows:
(1) doStartTag (); <xiazdong:hello>, if it is eval_body_include, continue; if Skip_body, then execute (4)
(2) the execution label body;
(3) If the Doafterbody is implemented, if the skip_body is returned, then the execution (4); If Eval_body_again is returned, the execution of Doafterbody is repeated;
(4) Doendtag (): Tag tail call;
2. Make no attribute label
In order to be clear, we illustrate by example.
code Example 1: implementation shows Hello world;
1. Preparation of Hellotag.java
Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
public class Hellotag extends tagsupport{public
int doStartTag () throws jspexception{out
= Super.pageContext.getOut ();
try{
out.println ("
Note the point:
(1) Javax.servlet.jsp.tagext.TagSupport, must remember;
(2) JspWriter out = Super.pageContext.getOut ();
(3) public int doStartTag () throws jspexception;
2. Preparation of Xiazdong.tld
TLD file is a label description file, is the core of the whole label programming, used to describe the name of the custom label, the implementation of the label, whether there is a label, description of attributes, etc.
A TLD file is actually similar to a tag library, which can describe many tags;
<tag>
<name>hello</name> <!--the name of the label is similar to that of forward-->
in <jsp:forward> <tag-class>org.tagext.HelloTag</tag-class> <!--label-->
<body-content>empty </body-content> <!--is there a label body-->
</tag>
Description of the form;
the template for the *.tld file is as follows: (because none of the previous content in the TLD file is important, you can copy the sticky tape directly)
<?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 http:// Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "
version=" 2.0 ">
<tlib-version>1.0</ tlib-version>
//Insert tag Description
</taglib>
3. Write Mappings in Web.xml
The purpose of writing mappings is similar to configuring a Web page with a mapping address for easy access;
For example, it was necessary to access/a/b/c/d/e/f/g/1.tld, this path is very troublesome, but if through mapping, we can only use "a" to represent a long list of file paths and names;
<jsp-config>
<taglib>
<taglib-uri>xiazdong</taglib-uri>
< taglib-location>/web-inf/classes/xiazdong.tld</taglib-location>
</taglib>
</ Jsp-config>
4. Write JSP pages and use custom labels
<%@ taglib prefix= "uri="%> prefix is similar to the previous mapping used in Jsp,uri in <jsp:forward>;
<%@ page contenttype= "text/html" pageencoding= "GBK"%> <%@ taglib prefix=
"Xiazdong" uri= "Xiazdong"%>
3. Make a property tag
In fact, the production of attributes and make no attributes are not much different,
1. You need to add attributes to the original Hellotag.java.
If you do something like: <xiazdong:hello id= "format="/> Properties, you need to add two attributes, ID attributes, and format properties to the Hellotag.java;
Shaped like:
Class Hellotag extends TagSupport
{
private String ID;
private String format;
Setter and getter Public
int doStartTag () throws jspexception{}
}
When the user uses a label and assigns a value to the ID and Format property, the setter method is automatically invoked to assign the ID and format in the Hellotag class;
2. Add attribute description in TLD file:
The property description is as follows:
<body-content>empty</body-content> back plus
<attribute>
<name>name</name> <!--property name-->
<required>true</required> <!--attribute is necessary-->
<rtexprvalue >empty</rtexprvalue> <!--support Expression language-->
</attribute>
Code instance:
Hellotag.java
Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
public class Hellotag extends tagsupport{
private String name;
Private String age;
Public String GetName () {return
name;
}
Public String Getage () {return age
;
}
public void SetName (String name) {
this.name = name;
}
public void Setage (String age) {
this.age = age;
}
public int doStartTag () throws jspexception{
JspWriter out = Super.pageContext.getOut ();
try{
out.println ("
xiazdong.tld need to add attribute description: Name, whether it is necessary, whether or not to support the expression language
<?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 http:// Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "
version=" 2.0 ">
<tlib-version>1.0</ tlib-version>
<short-name>x</short-name>
<tag>
<name>hello</name>
<tag-class>org.tagext.HelloTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>empty</rtexprvalue>
</attribute>
<attribute>
<name> age</name>
<required>true</required>
<rtexprvalue>empty</rtexprvalue>
</attribute>
</tag>
</taglib>
hellotag.jsp
<%@ page contenttype= "text/html" pageencoding= "GBK"%> <%@ taglib prefix=
"Xiazdong" uri= "Xiazdong"%>