第三篇 tag之间的嵌套和属性读取 
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里,极大的简化页面的编写。    
 
  |