dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | flex | CRM | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>jsp>文章内容
Taglib 原理和实现之嵌套和属性读取
来源:未知     作者:    
  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的,进行业务逻辑处理,然后输出到

  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 .io.IOException;

import x..jsp.Exception;
import x...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 Exception
 {
  this.value = ExpressionEvaluatorManager.evaluate("value", value.toString(), Object.class, this, pageContext);
 }
 public int doStartTag()
 {
  return EVAL_BODY_INCLUDE;
 }
 public int doEndTag()throws Exception
 {
  try
  {
   pageContext.getOut().print(output);
  }
  catch (IOException e)
  {
   throw new Exception(e);
  }
  return EVAL_PAGE;
 }
}

  编写 NestedOutputTag 如下:

package diegoyun;

import x..jsp.Exception;
import x...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 Exception
 {
  WithTag parent =(WithTag)getParent();
  if(parent==null)
   throw new Exception("Can not find parent Tag ");
   try
   {
    Object propertyValue = PropertyUtils.getProperty(parent.getValue(), property);
    parent.setOutput(propertyValue);
   }
   catch (Exception e)
   {
    throw new Exception(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></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="" %>
<%@ 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里显示到页面。如上面的例子,父 Tag 的 output 表示待打印的对象,通过 nestedoutTag 取得name的值,设置output,然后打印出来。

  通过支持El表达式和动态联结,Tag可以实现强大的处理功能。将逻辑都集中到Tag里,极大的简化页面的编写。

 

 

上一篇:JSP和Struts解决用户退出问题   下一篇:Taglib 原理和实现之支持El表达式
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·JSP入门实例教程
·什么是Servlet
·JSP常用内置对象使用说明
·JSP自定义标签由浅到深详细讲解
·JSP自定义标签由浅到深详细讲解
·Java Servlet技术
·Javax.servlet API的特征
·一个简单的jsp注册页面
·JSTL操作数据库示例
·JSP入门实例教程13-MVC模式
·JSP入门实例教程12-jstl处理数
·JSP入门实例教程11-jstl处理xml
  相关文章
·JSP和Struts解决用户退出问题
·Taglib 原理和实现之支持El表达
·Taglib原理和实现之循环的Tag
·Taglib 原理和实现之什么是Tagli
·JSP安全编程实例浅析
·深入剖析JSP和Servlet对中文的处
·利用JSP 2.0开发Web应用程序
·提升JSP应用程序的七大绝招
·用缓冲技术提高JSP程序的性能和
·jsp读取大对象CLOB并生成xml文件
·JSP/Servlet构建三层管理信息系
·一个读取xml文件内容的类
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763