2007-07-21 10:52:08| 分類: java和j2ee學習 |字號 訂閱html
顯然,這是一個嵌套Tag的問題。有三個Tag互相做用:最外層的Tag找到People對象,中間的Tag取得Collection,子Tag負責打印。java
例如:apache
<diego:withObject value=$>
<diego:withCollection property=men>
<diego:elementout property=name/>
</diego:withCollection>
</diego:withObject>jsp
思路以下:ide
1) 編寫WithObjectTag,負責從El表達式中取得對象oop
2) 編寫WithCollectionTag,負責從對象中取得 Collection ,遍歷 Collection ,每遍歷一次 Collection ,執行一次body學習
3) 編寫ElementoutTag ,把 Collection 中每一個men對象的 name 打印出來ui
2. 完整程序以下:this
在上例的diegoyun.vo包內,編寫 People 類lua
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
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=$>
<diego:withCollection property=men>
<diego:elementout property=name/>
<br>
</diego:withCollection>
</diego:withObject>
</body>
</html>
運行,則能夠看到:
Test loop tag: diego Zidane Rui