<diego:with object="${Man}">
<diego:output property="name"/>
</diego:with>
<diego:with object="${people}"> <!--表示取得一个对象-->
<diego:withCollection property="men"> <!--表示取得对象里的一个属性,这个属性是个 Collection,Collection里添加了许多man,每个man有名字和年龄-->
<diego:output property="name"/> <!--取得name属性并显示-->
</diego:withCollection>
</diego:with>
import org.apache.commons.beanutils.PropertyUtils;
......
property = PropertyUtils.getProperty(currentClass, propertyName);
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;
}
}
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;
}
}
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;
}
}
<!--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>
<%@ 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里,极大的简化页面的编写。