显然,这是一个嵌套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 |
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




