特别说明:如果能帮助到您,请您留下点滴痕迹,让我知道我的存在是有意义的;如果不能帮助到您,请接受我的歉意;
文章简介:本文说明带标签体的标签是如何定义。迭代器标签迭代指定集合元素;
=>IteratorTag.java
package lee;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
import java.sql.*;
import java.util.*;
/**
* IteratorTag_class
* 迭代器标签_带标签体;
* date 16:26 2014-10-28
* @author cyb_23
* @version 1.0
*/
public class IteratorTag extends SimpleTagSupport
{
private String collection;<span style="white-space:pre"> </span><span style="font-family: Arial;">//标签属性,用于指定需要被迭代的集合;</span>
private String item;<span style="white-space:pre"> </span><span style="font-family: Arial;">//标签属性,指定迭代集合元素,为集合元素指定的名称;</span><span style="white-space:pre">
</span>
//collection属性的setter和getter方法
public void setCollection(String collection)
{
this.collection = collection;
}
public String getCollection()
{
return this.collection;
}
//item属性的setter和getter方法
public void setItem(String item)
{
this.item = item;
}
public String getItem()
{
return this.item;
}
//标签的处理方法,简单标签处理类只需要重写doTag方法
public void doTag() throws JspException, IOException
{
//从page scope中获取属性名为collection的集合
Collection itemList = (Collection)getJspContext().getAttribute(collection);
//遍历集合
for (Object s : itemList)
{
//将集合的元素item设置到page 范围,以便让标签体能访问输出
getJspContext().setAttribute(item, s);
//输出标签体
getJspBody().invoke(null);<span style="white-space:pre"> </span>// <span style="font-family: Arial;">getJspBody()取得JspFragment对象,为标签体;</span>
}
}
}
=>mytaglib.tld
<?xml version="1.0" encoding="GBK"?>
<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 web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>mytaglib</short-name>
<!-- 标签库URI -->
<uri>http://blog.csdn.net/cyb_23/mytaglib</uri>
<!-- 定义迭代器标签 -->
<tag>
<name>iterator</name>
<tag-class>lee.IteratorTag</tag-class>
<!-- 定义_标签体不允许出现JSP脚本 -->
<body-content>scriptless</body-content>
<!-- 配置标签属性:collection -->
<attribute>
<name>collection</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<!-- 配置标签属性:item -->
<attribute>
<name>item</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
</taglib>
=>iteratorTag.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="java.util.*"%>
<!-- 导入标签库,指定mytag前缀的标签-->
<%@ taglib uri="http://blog.csdn.net/cyb_23/mytaglib" prefix="mytag"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> 带标签体的标签-迭代器标签 </title>
<meta name="website" content="<span style="font-family: Arial;">http://blog.csdn.net/cyb_23</span><span style="font-family: Arial;">" /></span>
</head>
<body>
<h2>带标签体的标签-迭代器标签</h2><hr/>
<%
//创建List对象
List<String> a = new ArrayList<String>();
a.add("疯狂Java");
a.add("www.crazyit.org");
a.add("java");
//将List对象放入page范围内
pageContext.setAttribute("a" , a);
%>
<table border="1" bgcolor="#aaaadd" width="300">
<!-- 使用迭代器标签,对a集合进行迭代 -->
<mytag:iterator collection="a" item="item">
<tr>
<td>${pageScope.item}</td>
<tr>
</mytag:iterator>
</table>
</body>
</html>