jsp创建一个带标签体的迭代器标签

来源:互联网 发布:centos安装hadoop 编辑:程序博客网 时间:2024/05/22 12:30

  1.定义迭代器标签处理类:

  /*

  * Java教程 by bbs.it-home.org

  */

  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

  {

  //指定集合

  private String collection;

  //指定集合元素

  private String item;

  //getter 和 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

  {

  //获取集合

  Collection itemList = (Collection) getJspContext().getAttribute(collection);

  //遍历集合

  for(Object s : itemList)

  {

  //将集合元素设置到page范围

  getJspContext().setAttribute(item, s);

  //输出标签体

  getJspBody().invoke(null);

  }

  }

  }

  复制代码

  2.建立tld文件

  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">

  1.0

  mytag

  /mytag

  iterator

  mckee.IteratorTag

  scriptless

  collection

  true

  true

  item

  true

  true

  复制代码

  3.在jsp页面中测试标签

  ${pageScope.item}

  复制代码


0 0