package com.rain.tag;
import Java.io.IOException;
import Java.util.Collection;
import Java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IterateTag extends BodyTagSupport {
private String name; //代表了在pageContext中表示的一个属性的名字
private Iterator it; //代表要迭代的内容
private String type; //表示it中对象的类型
public void setCollection(Collection collection){
if(collection.size()>0){
it=collection.iterator();
}
}
public void setName(String name){
this.name=name;
}
public void setType(String type){
this.type=type;
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
return continueNext(it);
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
try{
if(bodyContent!=null){
bodyContent.writeOut(bodyContent.getEnclosingWriter());
}
}catch(IOException e){
throw new JspTagException("IO Error:"+e.getMessage());
}
return EVAL_PAGE;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
if(it==null){
return SKIP_BODY;
}else{
return continueNext(it);
}
}
protected int continueNext(Iterator it)throws JspTagException{
if(it.hasNext()){
pageContext.setAttribute(name, it.next(), PageContext.PAGE_SCOPE);
return EVAL_BODY_AGAIN;
}else{
return SKIP_BODY;
}
}
}
由于BodyTagSupport类实现了TagSupport,而TagSupport又实现了TterationTag接口。故在开发迭代标签时可以直接从BodyTagSupport继承。
package com.rain.tag;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;
//TagExtraInfo用于提供一些在标签翻译时相关的信息
public class IterateTEI extends TagExtraInfo {
public IterateTEI(){
super();
}
@Override
public VariableInfo[] getVariableInfo(TagData arg0) {
// TODO Auto-generated method stub
return new VariableInfo[]{
new VariableInfo(arg0.getAttributeString("name"),
arg0.getAttributeString("type"),
true,
VariableInfo.NESTED)
};
}
}
需要说明的是VariableInfo的几个常数,如下所示:
1.NESTED:标签中的参数在starttag到endtag之间是有效的。
2.AT_BENGIN:标签中的参数在标签的开始到JSP页面结束是有效的。
3.AT_END:标签中的参数在标签的结束到JSP页面结束是有效的。
在标签的描述中,必须同时指定标签的实现类和IterateTEI类。
<tag>
<name>iterate</name>
<tag-class>com.rain.tag.IterateTag</tag-class>
<tei-class>com.rain.tag.IterateTEI</tei-class>
<body-content>jsp</body-content>
<attribute>
<name>collection</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<attribute>
<name>type</name>
<required>true</required>
</attribute>
</tag>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "
http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<taglib>
<taglib-uri>/demotag</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
</web-app>
<%@ page language="
Java" contentType="text/html; charset=UTF-8"%>
<jsp:directive.page import="
Java.util.Collection"/>
<%@ taglib uri="/demotag" prefix="mt" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
String one="one";
String two="two";
String three="three";
Collection list=new
Java.util.ArrayList();
list.add(one);
list.add(two);
list.add(three);
%>
<mt:iterate name="scott" type="String" collection="<%=list%>">
<%=scott%><br>
</mt:iterate>
</body>
</html>
运行结果:
one
two
three
posted on 2007-01-22 16:15
周锐 阅读(325)
评论(0) 编辑 收藏 所属分类:
Jsp