1. 新建LoopTag类,代码如下:
package bid.zhazhapan.fims.tag;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class LoopTag extends BodyTagSupport {
private String name;// 迭代出的对象在pageContext中的名字
private Collection collection;// 需要迭代的集合对象
private Iterator it;// 需要迭代的对象
private String type;// 在迭代器中对象的类型
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
// 将页面中的集合对象传入到程序中
public void setCollection(Collection collection) {
this.collection = collection;
// 生成迭代对象
if (collection.size() > 0) {
it = collection.iterator();
}
}
public int doStartTag() throws JspException {
// 如果集合没有内容,不执行标签体
if (it == null) {
return this.SKIP_BODY;
}
return this.EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException {
if (it.hasNext()) {
//在这里自定义输出的内容
pageContext.setAttribute(name, "<br/>" + it.next());
return this.EVAL_BODY_AGAIN;// 此返回值将反复调用此方法
}
return this.SKIP_BODY;
}
public int doEndTag() throws JspException {
if (bodyContent != null) {
try {
bodyContent.writeOut(bodyContent.getEnclosingWriter());
} catch (IOException e) {
throw new JspException("IO Error " + e.getMessage());
}
}
return this.EVAL_PAGE;
}
}
2. 新建LoopTEI类,代码如下:
package bid.zhazhapan.fims.tag;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;
public class LoopTEI extends TagExtraInfo {
// 覆盖方法,定义脚本变量的信息
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] { new VariableInfo(data.getAttributeString("name"), data.getAttributeString("type"), true, VariableInfo.NESTED)};
}
}
3. 新建“mytag.tld“xml配置文件(如何配置xml文件),内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<javaee:taglib version="2.1" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd ">
<javaee:tlib-version>1.0</javaee:tlib-version>
<javaee:short-name>demo</javaee:short-name>
<javaee:uri>/demo</javaee:uri>
<javaee:tag>
<javaee:name>loop</javaee:name>
<javaee:tag-class>bid.zhazhapan.fims.tag.LoopTag</javaee:tag-class>
<javaee:tei-class>bid.zhazhapan.fims.tag.LoopTEI</javaee:tei-class>
<javaee:body-content>JSP</javaee:body-content>
<javaee:attribute>
<javaee:name>name</javaee:name>
<javaee:required>true</javaee:required>
<javaee:rtexprvalue>true</javaee:rtexprvalue>
</javaee:attribute>
<javaee:attribute>
<javaee:name>type</javaee:name>
<javaee:required>true</javaee:required>
<javaee:rtexprvalue>true</javaee:rtexprvalue>
</javaee:attribute>
<javaee:attribute>
<javaee:name>collection</javaee:name>
<javaee:required>true</javaee:required>
<javaee:rtexprvalue>true</javaee:rtexprvalue>
</javaee:attribute>
</javaee:tag>
</javaee:taglib>
4. 测试标签,新建test.jsp,代码如下:
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="WEB-INF/tlds/mytag.tld" 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ArrayList ary = new ArrayList();
ary.add("apple");
ary.add("microsoft");
ary.add("google");
%>
<mytag:loop name="col" type="String" collection="<%=ary%>">${col }</mytag:loop>
</body>
</html>