目录结构

一、说明
此标签通过继承TagSupport类而不是通过继承BodyTagSupport类实现(书例题)
二、代码
IterateTag.java
package witersen;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
public class IterateTag extends TagSupport {
private String name; // 属性名称
private String scope; // 属性保存范围
private String id; // 每次迭代的对象
private Iterator<?> iter; // 所有接收到的数据
public int doStartTag() {
Object value = null;
if ("page".equals(this.scope)) {
value = super.pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
} else if ("request".equals(this.scope)) {
value = super.pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
} else if ("session".equals(this.scope)) {
value = super.pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
} else {
value = super.pageContext.getAttribute(name, pageContext.APPLICATION_SCOPE);
}
if (value != null && value instanceof List<?>) { // 如果是List接口实例
this.iter = ((List<?>)value).iterator(); // 将List接口进行详细转型
if (iter.hasNext()) {
super.pageContext.setAttribute(id, iter.next());
return TagSupport.EVAL_BODY_INCLUDE; // 执行标签体内容
} else {
return TagSupport.SKIP_BODY; // 退出标签执行
}
} else {// 不是List接口实例
return TagSupport.SKIP_BODY; // 退出标签执行
}
}
public int doAfterBody() {
if (iter.hasNext()) {
super.pageContext.setAttribute(id, iter.next());
return TagSupport.EVAL_BODY_AGAIN; // 重新执行标签体
} else {
return TagSupport.SKIP_BODY; // 退出标签执行
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
iter.tld
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>witersen</short-name>
<tag>
<name>iterate</name>
<tag-class>witersen.IterateTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="mytag" uri="/WEB-INF/iter.tld"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> all = new ArrayList<String>(); //定义List集合
all.add("内容1");
all.add("内容2");
all.add("内容3");
request.setAttribute("all", all);
%>
<mytag:iterate name="all" scope="request" id="url">
迭代标签体内容:${url}<br />
</mytag:iterate>
</body>
</html>
原创文章,作者:witersen,如若转载,请注明出处:https://www.witersen.com