欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp tag实例

程序员文章站 2022-03-24 23:45:58
   1、问题:在request里的people 对象,有个属性叫men ,men 是一个collection ,有许多个man 。现在,把collection里的man的名字都显示出...

 

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

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

  例如:

<diego:withobject value=$>

<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..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

 

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=$>

<diego:withcollection property=men>

<diego:elementout property=name/>

<br>

</diego:withcollection>

</diego:withobject>

</body>

</html>

  运行,则可以看到: 

test loop tag:

diego

zidane

rui