1、問題:在request裡有一個 Man 對象,它有兩個屬性:name和age。現在,我們想用一個嵌套的tag,父tag取得對象,子tag取得name屬性並顯示在頁面上。例如,它的形式如下:
<diego:with object="${Man}">
<diego:output property="name"/>
</diego:with> |
object 支援el運算式,表示取得 Man 對象。output的property表示從該對象取得名為name的屬性。
2、如何支援tag之間的嵌套
在子tag裡調用getParent 方法,可以得到父tag對象。用 findAncestorWithClass 方法,則可以通過遞迴找到想要找的tag。例如
<diego:with object="${people}"> <!--表示取得一個對象-->
<diego:withCollection property="men"> <!--表示取得對象裡的一個屬性,這個屬性是個 Collection,Collection裡添加了許多man,每個man有名字和年齡-->
<diego:output property="name"/> <!--取得name屬性並顯示-->
</diego:withCollection>
</diego:with> |
對於最內層的outputTag來說,調用getParent,可以得到 withCollectionTag,通過如findAncestorWithClass(this,WithTag.class)的方式,可以得到withTag。得到Tag之後,就可以取得Tag的屬性,進行商務邏輯處理,然後輸出到jsp
3、如何支援類屬性尋找功能
顯然,在上面的outputTag中,我們要根據屬性的名字,尋找類中有沒有這個屬性。然後取出屬性的值並顯示。通常,這可以編寫自己的反射函數來完成。更簡單的辦法,是通過 BeanUtil 的PropertyUtils方法來完成功能。BeanUtil 是apache上的一個開源項目。
樣本如下:
import org.apache.commons.beanutils.PropertyUtils;
......
property = PropertyUtils.getProperty(currentClass, propertyName); |
propertyName是待尋找屬性的名字,例如上面的"name",currentClass是待尋找的類,例如上面的People
記得把 commons-beanutils.jar添加到WEB-INF\\\\lib目錄下
4、現在讓我們實現開篇提出的問題,編寫WithTag如下:
package diegoyun;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
/**
* @author chenys
*/
public class WithTag extends BodyTagSupport
{
private Object value = null;
private Object output = null;
public void setOutput(Object output)
{
this.output = output;
}
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
{
try
{
pageContext.getOut().print(output);
}
catch (IOException e)
{
throw new JspException(e);
}
return EVAL_PAGE;
}
} |
編寫 NestedOutputTag 如下:
package diegoyun;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
/**
* @author chenys
*/
public class NestedOutputTag extends BodyTagSupport
{
private String property = null;
public void setProperty(String property)
{
this.property = property;
}
public int doEndTag()throws JspException
{
WithTag parent =(WithTag)getParent();
if(parent==null)
throw new JspException("Can not find parent Tag ");
try
{
Object propertyValue = PropertyUtils.getProperty(parent.getValue(), property);
parent.setOutput(propertyValue);
}
catch (Exception e)
{
throw new JspException(e);
}
return EVAL_PAGE;
}
} |
在包diegoyun下添加一個包vo,在vo下寫一個Man類:
package diegoyun.vo;
/**
* @author chenys
*/
public class Man
{
private String name = null;
private int age = 0;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
} |
寫TLD
<!--WithTag-->
<tag>
<name>with</name>
<tag-class>diegoyun.WithTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!--OutputTag3-->
<tag>
<name>nestedout</name>
<tag-class>diegoyun.NestedOutputTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
|
寫JSP頁面
<%@ page language="java" %>
<%@ page import="diegoyun.vo.*"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>
<html>
<body bgcolor="#FFFFFF">
<%
Man man = new Man();
man.setName("diego");
request.setAttribute("man",man);
%>
Test nested tag:
<br>
<diego:with value="${man}">
<diego:nestedout property="name"/>
</diego:with>
</body>
</html> |
運行頁面,則可以看到:
5、結束語:
上述例子簡單描繪了嵌套的Tag之間如何互動。通常子Tag負責取得資料,然後設定父Tag的屬性,最後在父Tag裡顯示到jsp頁面。如上面的例子,父 Tag 的 output 表示待列印的對象,通過 nestedoutTag 取得name的值,設定output,然後列印出來。
通過支援El運算式和動態屬性連接,Tag可以實現強大的處理功能。將邏輯都集中到Tag裡,極大的簡化頁面的編寫。