服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

taglib 原理和实现:第四章 循环的tag

第四篇 支持循环的tag

1。问题:在request里的 people 对象,有个属性叫 men ,men 是一个collection ,有许多个man 。现在,把 collection里的man的名字都显示出来

 显然,这是一个嵌套tag的问题。有三个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 java.util.collection;
public class people
{
 private collection men = null; 
 public collection getmen()
 {
  return men;
 }
 public void setmen(collection men)
 {
  this.men = men;
 }
}

编写 withobject ,这是从request里取得people对象的最外层tag

package diegoyun;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.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 jspexception
 {
  this.value = expressionevaluatormanager.evaluate(
            "value", value.tostring(), object.class, this, pagecontext);
 }
 public int dostarttag()
 {  
  return eval_body_include;
 }
 public int doendtag()throws jspexception
 {  
  return eval_page;
 }
}

 

编写withcollectiontag,该tag负责取得collection,并遍历执行子tag
package diegoyun;

import java.util.collection;
import java.util.iterator;

import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.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 jspexception {
 //取得父tag对象,并且得到collection
  withobjecttag parent = (withobjecttag) getparent();
  if (parent == null)
   throw new jspexception("parent tag is null");
  try {
   object propertyvalue = propertyutils.getproperty(parent.getvalue(),
     property);
   this.list = (collection) propertyvalue;
   if (list == null)
    throw new jspexception("collection is null");
  } catch (exception e) {
   throw new jspexception(e);
  }
 }

 public int dostarttag() throws jspexception {
 //设置第一个元素,然后执行子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 java.io.ioexception;

import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.tagext.tagsupport;

import org.apache.commons.beanutils.propertyutils;

public class elementoutputtag extends tagsupport
{
 private object propertyvalue  = null;
 public void setproperty(string property)throws jspexception
 {
  withcollectiontag parent = (withcollectiontag)getparent();
  if(parent == null)
   throw new jspexception("parent tag is null");
  try
  {
  //判断上层tag中是否存在该属性名称,如果存在,取得属性值,否则报错
   propertyvalue = propertyutils.getproperty(parent.getelement(), property);
  }
  catch (exception e)
  {
   throw new jspexception(e);
  }
 }
 public int doendtag()throws jspexception
 {
  try
  {
  //简单的把值打印到jsp页面
   pagecontext.getout().print(propertyvalue);
  }
  catch (ioexception e)
  {
   throw new jspexception(e);
  }
  return eval_page;
 }
}

编写tld
<!--withobjecttag-->
 <tag>
  <name>withobject</name>
  <tag-class>diegoyun.withobjecttag</tag-class>
  <body-content>jsp</body-content>
  <attribute>
   <name>value</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
 <!--withcollectiontag-->
 <tag>
  <name>withcollection</name>
  <tag-class>diegoyun.withcollectiontag</tag-class>
  <body-content>jsp</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>
 
编写jsp
<%@ page language="java" %>
<%@ page import="diegoyun.vo.*"%>
<%@ page import="java.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);
%>
test 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

扫描关注微信公众号