1、問題描述:在 request 中的 People 物件有個屬性叫 men,men 是一個 Collection,裡面裝著許許多多的 man。現在,要把 Collection 裡每個 man 的名字都顯示出來。
顯然,這是一個巢狀 Tag 的問題。有三個 Tag 互相作用:最外層的 Tag 找到 People 物件,中間的 Tag 取得 Collection,子 Tag 負責列印。
例如:
<diego:withObject value="${people}">
<diego:withCollection property="men">
<diego:elementout property="name"/>
</diego:withCollection>
</diego:withObject>
思路如下:
1)撰寫 WithObjectTag,負責從 EL 表達式中取得物件。
2)撰寫 WithCollectionTag,負責從物件中取得 Collection,遍歷 Collection,每遍歷一次 Collection 就執行一次 body。
3)撰寫 ElementoutTag,把 Collection 中每個 men 物件的 name 印出來。
2. 完整程式如下:
在上例的 diegoyun.vo 套件內,撰寫 People 類別:
package diegoyun.vo;
import java.util.Collection;
public class People
{
private Collection men = null;
public Collection getMen()
{
return men;
}
public void setMen(Collection men)
{
this.men = men;
}
}
撰寫 withObject,這是最外層、負責從 request 取得 People 物件的 Tag:
package diegoyun;
import javax.servlet.jsp.JSPException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
public class WithObjectTag extends BodyTagSupport
{
private Object value = null;
public Object getValue()
{
return value;
}
public void setValue(Object value)throws JSPException
{
this.value = ExpressionEvaluatorManager.evaluate("value", value.toString(), Object.class, this, pageContext);
}
public int doStartTag()
{
return EVAL_BODY_INCLUDE;
}
public int doEndTag()throws JSPException
{
return EVAL_PAGE;
}
}
撰寫 WithCollectionTag,該 Tag 負責取得 Collection,並遍歷執行子 Tag:
package diegoyun;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.jsp.JSPException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
public class WithCollectionTag extends BodyTagSupport {
private Object element = null;
private Collection list = null;
private Iterator iterator = null;
public Object getElement() {
return element;
}
public void setProperty(String property) throws JSPException {
//取得父 Tag 物件,並且得到 Collection
WithObjectTag parent = (WithObjectTag) getParent();
if (parent == null)
throw new JSPException("parent tag is null");
try {
Object propertyValue = PropertyUtils.getProperty(parent.getValue(),property);
this.list = (Collection) propertyValue;
if (list == null)
throw new JSPException("Collection is null");
} catch (Exception e) {
throw new JSPException(e);
}
}
public int doStartTag() throws JSPException {
//設定第一個元素,然後執行子 Tag
iterator = list.iterator();
if (iterator.hasNext())
element = iterator.next();
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() {
if (iterator.hasNext()) {
//如果還存在子元素,設定子元素,並且再次執行子 Tag
//循環由此而來
//否則不再執行子 Tag
element = iterator.next();
return EVAL_BODY_AGAIN;
}
else
return EVAL_PAGE;
}
}
撰寫 ElementOutputTag:
package diegoyun;
import java.io.IOException;
import javax.servlet.jsp.JSPException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.beanutils.PropertyUtils;
public class ElementOutputTag extends TagSupport
{
private Object propertyValue = null;
public void setProperty(String property)throws JSPException
{
WithCollectionTag parent = (WithCollectionTag)getParent();
if(parent == null)
throw new JSPException("parent tag is null");
try
{
//判斷上層 tag 中是否存在該屬性名稱,如果存在,取得屬性值,否則報錯
propertyValue = PropertyUtils.getProperty(parent.getElement(), property);
}
catch (Exception e)
{
throw new JSPException(e);
}
}
public int doEndTag()throws JSPException
{
try
{
//簡單的把值印到 JSP 頁面
pageContext.getOut().print(propertyValue);
}
catch (IOException e)
{
throw new JSPException(e);
}
return EVAL_PAGE;
}
}
撰寫 tld:
<!--WithObjectTag--> <tag> <name>withObject</name> <tag-class>diegoyun.WithObjectTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!--WithCollectionTag--> <tag> <name>withCollection</name> <tag-class>diegoyun.WithCollectionTag</tag-class> <body-content>JSP</body-content> <attribute> <name>property</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!--ElementOutputTag--> <tag> <name>elementout</name> <tag-class>diegoyun.ElementOutputTag</tag-class> <body-content>empty</body-content> <attribute> <name>property</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
撰寫 JSP:
<%@ page language="java" %>
<%@ page import="diegoyun.vo.*"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>
<html>
<body bgcolor="#FFFFFF">
<%
Collection c = new ArrayList();
Man man1 = new Man();
man1.setName("diego");
c.add(man1);
Man man2 = new Man();
man2.setName("Zidane");
c.add(man2);
Man man3 = new Man();
man3.setName("Rui");
c.add(man3);
People p =new People();
p.setMen(c);
request.setAttribute("people",p);
%>
Test loop tag:
<br>
<diego:withObject value="${people}">
<diego:withCollection property="men">
<diego:elementout property="name"/>
<br>
</diego:withCollection>
</diego:withObject>
</body>
</html>
執行後,可以看到:
Test loop tag:
diego
Zidane
Rui