1. Define the iterator tag processing class:
The code is as follows: |
Copy code |
Package mckee; Import java. io. IOException; Import java. util. Collection; Import javax. servlet. jsp. JspException; Import javax. servlet. jsp. tagext. SimpleTagSupport; Public class IteratorTag extends SimpleTagSupport { // Specify the set Private String collection; // Specify the set element Private String item; // Getter and setter Public String getCollection (){ Return collection; } Public void setCollection (String collection ){ This. collection = collection; } Public String getItem (){ Return item; } Public void setItem (String item ){ This. item = item; } Public void doTag () throws JspException, IOException { // Obtain the set Collection itemList = (Collection) getJspContext (). getAttribute (collection ); // Traverse the set For (Object s: itemList) { // Set the set element to the page range GetJspContext (). setAttribute (item, s ); // Output Tag body GetJspBody (). invoke (null ); } } } |
2. Create a tld file
The code is as follows: |
Copy code |
<? 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> mytag </short-name> <Uri>/mytag </uri> <! -- This is very important. The jsp page locates the tag library based on the uri --> <Tag> <Name> iterator </name> <! -- Define the tag name --> <Tag-class> mckee. IteratorTag </tag-class> <! -- Define tag processing class --> <Body-content> scriptless </body-content> <! -- Define the TAG Body --> <! -- Configure the tag attributes of collection and item --> <Attribute> <Name> collection </name> <Required> true </required> <Fragment> true </fragment> </Attribute> <Attribute> <Name> item </name> <Required> true </required> <Fragment> true </fragment> </Attribute> </Tag> </Taglib>
|
2. Create a tld file
The code is as follows: |
Copy code |
<? 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> mytag </short-name> <Uri>/mytag </uri> <! -- This is very important. The jsp page locates the tag library based on the uri --> <Tag> <Name> iterator </name> <! -- Define the tag name --> <Tag-class> mckee. IteratorTag </tag-class> <! -- Define tag processing class --> <Body-content> scriptless </body-content> <! -- Define the TAG Body --> <! -- Configure the tag attributes of collection and item --> <Attribute> <Name> collection </name> <Required> true </required> <Fragment> true </fragment> </Attribute> <Attribute> <Name> item </name> <Required> true </required> <Fragment> true </fragment> </Attribute> </Tag> </Taglib>
|
3. Test tags on the jsp page
The code is as follows: |
Copy code |
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ Page import = "java. util. *" %> <% @ Taglib uri = "/mytag" prefix = "mytag" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> <Html> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> <Title> powered by http://www.111cn.net </title> </Head> <Body> <% List <String> arr = new ArrayList <String> (); Arr. add ("java tutorial "); Arr. add ("http://www.111cn.net "); PageContext. setAttribute ("arr", arr ); %> <Mytag: iterator collection = "arr" item = "item"> $ {Pagination. item} <br> </Mytag: iterator> </Body> </Html>
|