欢迎光临
我们一直在努力

Taglib原理和实现之循环的Tag-JSP教程,资料/其它

建站超值云服务器,限时71元/月

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Taglib原理和实现之循环的Tag-JSP教程,资料/其它
分享到: 更多 (0)

相关推荐

  • 暂无文章