此文章由教程中國(
http://www.upschool.com.cn)提供 如果您覺得可以,請推薦給同好.本站將在近期正式對外開放教程貼吧論壇,將設(shè)有專家欄,如果您有問題,可隨時得到解決.
1、問題:在request里有一個 Man 對象,它有兩個屬性:name和age?,F(xiàn)在,我們想用一個嵌套的tag,父tag取得對象,子tag取得name屬性并顯示在頁面上。例如,它的形式如下:
<diegwith object="${Man}">
<diegoutput property="name"/>
</diegwith>
object 支持el表達式,表示取得 Man 對象。output的property表示從該對象取得名為name的屬性。
2、如何支持tag之間的嵌套
在子tag里調(diào)用getParent 方法,可以得到父tag對象。用 findAncestorWithClass 方法,則可以通過遞歸找到想要找的tag。例如
<diegwith object="${people}"> <!--表示取得一個對象-->
<diegwithCollection property="men"> <!--表示取得對象里的一個屬性,這個屬性是個 Collection,Collection里添加了許多man,每個man有名字和年齡-->
<diegoutput property="name"/> <!--取得name屬性并顯示-->
</diegwithCollection>
</diegwith>
對于最內(nèi)層的outputTag來說,調(diào)用getParent,可以得到 withCollectionTag,通過如findAncestorWithClass(this,WithTag.class)的方式,可以得到withTag。得到Tag之后,就可以取得Tag的屬性,進行業(yè)務(wù)邏輯處理,然后輸出到j(luò)sp
3、如何支持類屬性查找功能
顯然,在上面的outputTag中,我們要根據(jù)屬性的名字,查找類中有沒有這個屬性。然后取出屬性的值并顯示。通常,這可以編寫自己的反射函數(shù)來完成。更簡單的辦法,是通過 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、現(xiàn)在讓我們實現(xiàn)開篇提出的問題,編寫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>
<diegwith value="${man}">
<diegnestedout property="name"/>
</diegwith>
</body>
</html>
運行頁面,則可以看到:
Test nested tag:
diego
5、結(jié)束語:
上述例子簡單描繪了嵌套的Tag之間如何交互。通常子Tag負責取得數(shù)據(jù),然后設(shè)置父Tag的屬性,最后在父Tag里顯示到j(luò)sp頁面。如上面的例子,父 Tag 的 output 表示待打印的對象,通過 nestedoutTag 取得name的值,設(shè)置output,然后打印出來。
通過支持El表達式和動態(tài)屬性聯(lián)結(jié),Tag可以實現(xiàn)強大的處理功能。將邏輯都集中到Tag里,極大的簡化頁面的編寫。