Create an iterator tag with a tag in jsp

Source: Internet
Author: User
Tags tag name tld

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>

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.