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>
|
运行页面,则可以看到:
test nested tag:
diego
5.结束语:
上述例子简单描绘了嵌套的tag之间如何交互。通常子tag负责取得数据,然后设置父tag的属性,最后在父tag里显示到jsp页面。如上面的例子,父 tag 的 output 表示待打印的对象,通过 nestedouttag 取得name的值,设置output,然后打印出来。
通过支持el表达式和动态属性联结,tag可以实现强大的处理功能。将逻辑都集中到tag里,极大的简化页面的编写。