In JSP, the standard simplifies the development of the tag library. To enable custom tags in JSP2, you only need to perform the following three steps:
I. Development of custom tag processing class
2. Create a *. tld File
3. Use custom tags in JSP files
The custom label class should inherit a parent class: javax. servlet. jsp. tagext. SimpleTagSupport. In addition, the JSP custom label class also has the following requirements:
1. If the label class contains attributes, each attribute must provide the corresponding getter and setter methods.
2. Rewrite the doTag () method to generate page content
Start tag development as follows:
I. Develop a custom tag processing class ForeachTag to implement the iterative List set and array functions.
Public class ForeachTag extends SimpleTagSupport {
// Tag attributes
Private String items;
Private String var;
// The getter setter method is omitted here
@ Override
Public void doTag () throws JspException, IOException {
Iterator ite = null;
Object tempItem = getJspContext (). getAttribute (items );
// If it is a set
If (tempItem instanceof Collection ){
Ite = (Collection) tempItem). iterator ();
}
// If it is an array
Else if (tempItem instanceof Object []) {
Ite = Arrays. asList (Object []) tempItem). iterator ();
} Else {
Throw new RuntimeException ("cannot be converted ");
}
// Iteration
While (ite. hasNext ()){
Object obj = ite. next ();
GetJspContext (). setAttribute (var, obj );
// Output TAG body
GetJspBody (). invoke (null );
}
}
}
2. Develop *. tld files. Create the mytaglib. tld file in the web-inf directory. The content is as follows:
<? 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">
<Description> this is my tag library Definition </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> mytaglib </short-name>
<Uri> http: // 127.0.0.0/servlet/mytaglib </uri>
<Tag>
<Name> foreach </name>
<Tag-class> taglib. ForeachTag </tag-class>
<Body-content> scriptless </body-content>
<! -- Specify the items attribute and var attribute in the foreach loop -->
<Attribute>
<Name> items </name>
<Required> true </required>
<Fragment> true </fragment>
</Attribute>
<Attribute>
<Name> var </name>
<Required> true </required>
<Fragment> true </fragment>
</Attribute>
</Tag>
</Taglib>
3. Develop JSP and use custom tags in JSP to implement list set Iteration
<% @ Page contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" errorPage = "" %>
<% @ Page import = "java. util. *" %>
<% @ Taglib uri = "http: // 127.0.0.0/servlet/mytaglib" prefix = "mytag" %>
<%
List <String> lists = new ArrayList <String> ();
Lists. add ("Crazy Java handout ");
Lists. add ("lightweight JavaEE enterprise applications ");
Lists. add ("Crazy Ajax handout ");
PageContext. setAttribute ("lists", lists );
String [] ary = new String [3];
Ary [0] = "A"; www.2cto.com
Ary [1] = "B ";
Ary [2] = "C ";
PageContext. setAttribute ("ary", ary );
%>
<Html>
<Head>
<Title> custom tag-foreach-Like tag function: cyclic List set, one-dimensional array </title>
</Head>
<Body>
<H3> here We iterate over a set <Table style = "border: solid 1px black; width = 400xp;">
<Mytag: foreach items = "lists" var = "item">
<Tr>
<Td >$ {item} </td>
</Tr>
</Mytag: foreach>
</Table>
<Br/>
<H3> here the iterations are arrays <Mytag: foreach items = "ary" var = "item">
$ {Item} & nbsp;
</Mytag: foreach>
</Body>
</Html>
Note: Use <% @ taglib %> to import custom development labels. The uri path is the same as the uri path specified in the mytaglib. tld file.