Taglib原理和實現之嵌套和屬性讀取
1、問題:在request里有一個 Man 對象,它有兩個屬性:name和age。現在,我們想用一個嵌套的tag,父tag取得對象,子tag取得name屬性并顯示在頁面上。例如,它的形式如下:
object 支持el表達式,表示取得 Man 對象。output的property表示從該對象取得名為name的屬性。
2、如何支持tag之間的嵌套
在子tag里調用getParent 方法,可以得到父tag對象。用 findAncestorWithClass 方法,則可以通過遞歸找到想要找的tag。例如
對于最內層的outputTag來說,調用getParent,可以得到 withCollectionTag,通過如findAncestorWithClass(this,WithTag.class)的方式,可以得到withTag。得到Tag之后,就可以取得Tag的屬性,進行業務邏輯處理,然后輸出到jsp
3、如何支持類屬性查找功能
顯然,在上面的outputTag中,我們要根據屬性的名字,查找類中有沒有這個屬性。然后取出屬性的值并顯示。通常,這可以編寫自己的反射函數來完成。更簡單的辦法,是通過 BeanUtil 的PropertyUtils方法來完成功能。BeanUtil 是apache上的一個開源項目。
示例如下:
propertyName是待查找屬性的名字,例如上面的"name",currentClass是待查找的類,例如上面的People
記得把 commons-beanutils.jar添加到WEB-INFlib目錄下
4、現在讓我們實現開篇提出的問題,編寫WithTag如下:
編寫 NestedOutputTag 如下:
在包diegoyun下添加一個包vo,在vo下寫一個Man類:
寫TLD
寫JSP頁面
運行頁面,則可以看到:
5、結束語:
上述例子簡單描繪了嵌套的Tag之間如何交互。通常子Tag負責取得數據,然后設置父Tag的屬性,最后在父Tag里顯示到jsp頁面。如上面的例子,父 Tag 的 output 表示待打印的對象,通過 nestedoutTag 取得name的值,設置output,然后打印出來。
通過支持El表達式和動態屬性聯結,Tag可以實現強大的處理功能。將邏輯都集中到Tag里,極大的簡化頁面的編寫。
| <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-INFlib目錄下
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> |
運行頁面,則可以看到:
| Test nested tag: diego |
5、結束語:
上述例子簡單描繪了嵌套的Tag之間如何交互。通常子Tag負責取得數據,然后設置父Tag的屬性,最后在父Tag里顯示到jsp頁面。如上面的例子,父 Tag 的 output 表示待打印的對象,通過 nestedoutTag 取得name的值,設置output,然后打印出來。
通過支持El表達式和動態屬性聯結,Tag可以實現強大的處理功能。將邏輯都集中到Tag里,極大的簡化頁面的編寫。