dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>jsp>文章内容
Taglib原理和实现之循环的Tag
来源:未知     作者:    
  1、问题:在request里的 People 对象,有个叫 men ,men 是一个Collection ,有许多个man 。现在,把 collection里的man的名字都显示出来。

  显然,这是一个嵌套的问题。有三个Tag互相作用:最外层的Tag找到People对象,中间的Tag取得Collection,子Tag负责打印。

  例如:

<diego:withObject value="${people}">
 <diego:withCollection property="men">
  <diego:elementout property="name"/>
 </diego:withCollection>
</diego:withObject>

  思路如下:

  1) 编写WithObjectTag,负责从El表达式中取得对象

  2) 编写WithCollectionTag,负责从对象中取得 Collection ,遍历 Collection ,每遍历一次 Collection ,执行一次body

  3) 编写ElementoutTag ,把 Collection 中每个men对象的 name 打印出来

  2. 完整程序如下:

  在上例的diegoyun.vo包内,编写 People 类

package diegoyun.vo;
import .util.Collection;
public class People
{
 private Collection men = null;
 public Collection getMen()
 {
  return men;
 }
 public setMen(Collection men)
 {
  this.men = men;
 }
}

  编写 withObject ,这是从request里取得People对象的最外层Tag

package diegoyun;
import x..jsp.Exception;
import x...tagext.BodyTagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
public class WithObjectTag extends BodyTagSupport
{
 private Object value = null;

 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
 {
  return EVAL_PAGE;
 }
}

  编写WithCollectionTag,该Tag负责取得Collection,并遍历执行子Tag

package diegoyun;

import .util.Collection;
import .util.Iterator;

import x..jsp.Exception;
import x...tagext.BodyTagSupport;

import org.apache.commons.beanutils.PropertyUtils;

public class WithCollectionTag extends BodyTagSupport {
 private Object element = null;

 private Collection list = null;

 private Iterator iterator = null;

 public Object getElement() {
  return element;
 }

 public void setProperty(String property) throws Exception {
  //取得父Tag对象,并且得到Collection
  WithObjectTag parent = (WithObjectTag) getParent();
  if (parent == null)
   throw new Exception("parent tag is null");
   try {
    Object propertyValue = PropertyUtils.getProperty(parent.getValue(),property);
    this.list = (Collection) propertyValue;
    if (list == null)
     throw new Exception("Collection is null");
   } catch (Exception e) {
    throw new Exception(e);
  }
 }

 public int doStartTag() throws Exception {
  //设置第一个元素,然后执行子Tag
  iterator = list.iterator();
  if (iterator.hasNext())
   element = iterator.next();
   return EVAL_BODY_INCLUDE;
 }

 public int doAfterBody() {
  if (iterator.hasNext()) {
   //如果还存在子元素,设置子元素,并且再次执行子Tag
   //循环由此而来
   //否则不再执行子Tag
   element = iterator.next();
   return EVAL_BODY_AGAIN;
  }
  else
   return EVAL_PAGE;
 }
}

  编写 ElementOutputTag

package diegoyun;
import .io.IOException;

import x..jsp.Exception;
import x...tagext.TagSupport;

import org.apache.commons.beanutils.PropertyUtils;

public class ElementOutputTag extends TagSupport
{
 private Object propertyValue = null;
 public void setProperty(String property)throws Exception
 {
  WithCollectionTag parent = (WithCollectionTag)getParent();
  if(parent == null)
   throw new Exception("parent tag is null");
  try
  {
   //判断上层tag中是否存在该属性名称,如果存在,取得值,否则报错
   propertyValue = PropertyUtils.getProperty(parent.getElement(), property);
  }
  catch (Exception e)
  {
   throw new Exception(e);
  }
 }
 public int doEndTag()throws Exception
 {
  try
  {
   //简单的把值打印到页面
   pageContext.getOut().print(propertyValue);
  }
  catch (IOException e)
  {
   throw new Exception(e);
  }
  return EVAL_PAGE;
 }
}

  编写

<!--WithObjectTag-->
<tag>
 <name>withObject</name>
 <tag-class>diegoyun.WithObjectTag</tag-class>
 <body-content></body-content>
 <attribute>
  <name>value</name>
  <required>false</required>
  <rtexprvalue></rtexprvalue>
 </attribute>
</tag>
<!--WithCollectionTag-->
<tag>
 <name>withCollection</name>
 <tag-class>diegoyun.WithCollectionTag</tag-class>
 <body-content></body-content>
 <attribute>
  <name>property</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
</tag>
<!--ElementOutputTag-->
<tag>
 <name>elementout</name>
 <tag-class>diegoyun.ElementOutputTag</tag-class>
 <body-content>empty</body-content>
 <attribute>
  <name>property</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
</tag>

  编写

<%@ page language="" %>
<%@ page import="diegoyun.vo.*"%>
<%@ page import=".util.*"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>

<html>
<body bgcolor="#FFFFFF">
<%
 Collection c = new ArrayList();

 Man man1 = new Man();
 man1.setName("diego");
 c.add(man1);

 Man man2 = new Man();
 man2.setName("Zidane");
 c.add(man2);

 Man man3 = new Man();
 man3.setName("Rui");
 c.add(man3);

 People p =new People();
 p.setMen(c);
 request.setAttribute("people",p);
%>
loop tag:
<br>
<diego:withObject value="${people}">
<diego:withCollection property="men">
<diego:elementout property="name"/>
<br>
</diego:withCollection>
</diego:withObject>
</body>
</html>

  运行,则可以看到: 

Test loop tag:
diego
Zidane
Rui

 

 

上一篇:JSP安全编程实例浅析   下一篇:JSP和Struts解决用户退出问题
[收藏] [推荐] [评论(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安全编程实例浅析
·JSP和Struts解决用户退出问题
·利用JSP 2.0开发Web应用程序
·Taglib 原理和实现之嵌套和属性
·用缓冲技术提高JSP程序的性能和
·Taglib 原理和实现之支持El表达
·JSP/Servlet构建三层管理信息系
·Taglib 原理和实现之什么是Tagli
·JSP/Servlet的重定向技术综述
·深入剖析JSP和Servlet对中文的处
·JSP报表打印的一种简单解决方案
·提升JSP应用程序的七大绝招
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763