开发一个迭代标签,此标签只可以对list进行迭代(学习)。
IterateTag.java:
package com.keith.tag;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
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;
@Override
public int doStartTag() throws JspException {
Object value = null;
// 是否是page范围
if ("page".equals(this.scope)) {
value = super.pageContext
.getAttribute(name, PageContext.PAGE_SCOPE);
} else if ("request".equals(this.scope)) {
// 是否是request范围
value = super.pageContext.getAttribute(name,
PageContext.REQUEST_SCOPE);
} else if ("session".equals(this.scope)) {
// 是否是session范围
value = super.pageContext.getAttribute(name,
PageContext.SESSION_SCOPE);
} else {
// 是否是application范围
value = super.pageContext.getAttribute(name,
PageContext.APPLICATION_SCOPE);
}
//如果是List接口的实例
if (value != null && value instanceof List<?>) {
//像List接口进行向下转型
this.iter = ((List<?>) value).iterator();
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;
}
}
@Override
public int doAfterBody() throws JspException {
//判断是否还有内容
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;
}
public Iterator<?> getIter() {
return iter;
}
public void setIter(Iterator<?> iter) {
this.iter = iter;
}
}
iteratetag.tld:
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" 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" version="2.1"> <!-- 标签库的版本 --> <tlib-version>1.0</tlib-version> <!-- 为标签苦在TLD中的描述名称 --> <short-name>iteratetag</short-name> <tag> <!-- 表示标签在JSP中的使用名称 --> <name>iterate</name> <!-- 表示这个标签所这项的Class --> <tag-class>com.keith.tag.IterateTag</tag-class> <!-- 标签体内容为空 --> <body-content>JSP</body-content> <attribute> <!-- format为属性名 --> <name>id</name> <!-- 表示此值必须设置 --> <required>true</required> <!-- 表示属性值是请求时表达式的结果 --> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <!-- format为属性名 --> <name>name</name> <!-- 表示此值必须设置 --> <required>true</required> <!-- 表示属性值是请求时表达式的结果 --> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <!-- format为属性名 --> <name>scope</name> <!-- 表示此值必须设置 --> <required>true</required> <!-- 表示属性值是请求时表达式的结果 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
web.xml:
<taglib> <taglib-uri>iterate</taglib-uri> <taglib-location>/WEB-INF/iteratetag.tld</taglib-location> </taglib>
index.jsp:
<%@ page import="java.util.*" %>
<%@ taglib prefix="iteratetag" uri="iterate"%>
<body>
<%
List<String> all = new ArrayList<String>();
all.add("java");
all.add("linux");
all.add("C");
request.setAttribute("all",all);
%>
<iteratetag:iterate name="all" scope="request" id="url">
${url }<br />
</iteratetag:iterate>
</body>