📄 taglib原理和实现之循环的tag - fanqiang_com.htm
字号:
EVAL_BODY_INCLUDE;<BR> }<BR> public int doEndTag()throws
JspException<BR> { <BR> return EVAL_PAGE;<BR> }<BR>}
</TD></TR></TBODY></TABLE><BR> 编写WithCollectionTag,该Tag负责取得Collection,并遍历执行子Tag<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf
border=1>
<TBODY>
<TR>
<TD>package diegoyun;<BR><BR>import
java.util.Collection;<BR>import
java.util.Iterator;<BR><BR>import
javax.servlet.jsp.JspException;<BR>import
javax.servlet.jsp.tagext.BodyTagSupport;<BR><BR>import
org.apache.commons.beanutils.PropertyUtils;<BR><BR>public
class WithCollectionTag extends BodyTagSupport {<BR> private
Object element = null;<BR><BR> private Collection list =
null;<BR><BR> private Iterator iterator = null;<BR><BR> public
Object getElement() {<BR> return
element;<BR> }<BR><BR> public void setProperty(String
property) throws JspException
{<BR> //取得父Tag对象,并且得到Collection<BR> WithObjectTag parent =
(WithObjectTag) getParent();<BR> if (parent ==
null)<BR> throw new JspException("parent tag is
null");<BR> try {<BR> Object propertyValue =
PropertyUtils.getProperty(parent.getValue(),property);<BR> this.list
= (Collection) propertyValue;<BR> if (list ==
null)<BR> throw new JspException("Collection is
null");<BR> } catch (Exception e) {<BR> throw new
JspException(e);<BR> }<BR> }<BR><BR> public int doStartTag()
throws JspException {<BR> //设置第一个元素,然后执行子Tag<BR> iterator =
list.iterator();<BR> if (iterator.hasNext())<BR> element =
iterator.next();<BR> return
EVAL_BODY_INCLUDE;<BR> }<BR><BR> public int doAfterBody()
{<BR> if (iterator.hasNext())
{<BR> //如果还存在子元素,设置子元素,并且再次执行子Tag<BR> //循环由此而来<BR> //否则不再执行子Tag<BR> element
= iterator.next();<BR> return
EVAL_BODY_AGAIN;<BR> }<BR> else<BR> return
EVAL_PAGE;<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> 编写
ElementOutputTag<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf
border=1>
<TBODY>
<TR>
<TD>package diegoyun;<BR>import
java.io.IOException;<BR><BR>import
javax.servlet.jsp.JspException;<BR>import
javax.servlet.jsp.tagext.TagSupport;<BR><BR>import
org.apache.commons.beanutils.PropertyUtils;<BR><BR>public
class ElementOutputTag extends TagSupport<BR>{<BR> private
Object propertyValue = null;<BR> public void
setProperty(String property)throws
JspException<BR> {<BR> WithCollectionTag parent =
(WithCollectionTag)getParent();<BR> if(parent ==
null)<BR> throw new JspException("parent tag is
null");<BR> try<BR> {<BR> //判断上层tag中是否存在该属性名称,如果存在,取得属性值,否则报错<BR> propertyValue
= PropertyUtils.getProperty(parent.getElement(),
property);<BR> }<BR> catch (Exception e)<BR> {<BR> throw
new JspException(e);<BR> }<BR> }<BR> public int
doEndTag()throws
JspException<BR> {<BR> try<BR> {<BR> //简单的把值打印到jsp页面<BR> pageContext.getOut().print(propertyValue);<BR> }<BR> catch
(IOException e)<BR> {<BR> throw new
JspException(e);<BR> }<BR> return
EVAL_PAGE;<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> 编写tld<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf
border=1>
<TBODY>
<TR>
<TD><!--WithObjectTag--><BR><tag><BR> <name>withObject</name><BR> <tag-class>diegoyun.WithObjectTag</tag-class><BR> <body-content>JSP</body-content><BR> <attribute><BR> <name>value</name><BR> <required>false</required><BR> <rtexprvalue>true</rtexprvalue><BR> </attribute><BR></tag><BR><!--WithCollectionTag--><BR><tag><BR> <name>withCollection</name><BR> <tag-class>diegoyun.WithCollectionTag</tag-class><BR> <body-content>JSP</body-content><BR> <attribute><BR> <name>property</name><BR> <required>false</required><BR> <rtexprvalue>true</rtexprvalue><BR> </attribute><BR></tag><BR><!--ElementOutputTag--><BR><tag><BR> <name>elementout</name><BR> <tag-class>diegoyun.ElementOutputTag</tag-class><BR> <body-content>empty</body-content><BR> <attribute><BR> <name>property</name><BR> <required>false</required><BR> <rtexprvalue>true</rtexprvalue><BR> </attribute><BR></tag></TD></TR></TBODY></TABLE><BR> 编写jsp<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf
border=1>
<TBODY>
<TR>
<TD><%@ page language="java" %><BR><%@ page
import="diegoyun.vo.*"%><BR><%@ page
import="java.util.*"%><BR><%@ taglib
uri="/WEB-INF/tlds/diego.tld"
prefix="diego"%><BR><BR><html><BR><body
bgcolor="#FFFFFF"><BR><%<BR> Collection c = new
ArrayList();<BR><BR> Man man1 = new
Man();<BR> man1.setName("diego");<BR> c.add(man1);<BR><BR> Man
man2 = new
Man();<BR> man2.setName("Zidane");<BR> c.add(man2);<BR><BR> Man
man3 = new
Man();<BR> man3.setName("Rui");<BR> c.add(man3);<BR><BR> People
p =new
People();<BR> p.setMen(c);<BR> request.setAttribute("people",p);<BR>%><BR>Test
loop tag:<BR><br><BR><diego:withObject
value="${people}"><BR><diego:withCollection
property="men"><BR><diego:elementout
property="name"/><BR><br><BR></diego:withCollection><BR></diego:withObject><BR></body><BR></html></TD></TR></TBODY></TABLE><BR> 运行,则可以看到: <BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf
border=1>
<TBODY>
<TR>
<TD>Test loop tag: <BR>diego <BR>Zidane
<BR>Rui</TD></TR></TBODY></TABLE></SPAN><!-- 正文end --><BR>(http://www.fanqiang.com)<BR></FONT><BR><FONT
color=#999999><SMALL>原文链接:<A
href="http://news.chinabyte.com/SoftChannel/72342371945283584/20041229/1894523.shtml"
target=_blank>http://news.chinabyte.com/SoftChannel/72342371945283584/20041229/1894523.shtml</A></SMALL></FONT>
<BR></FONT></TD></TR></TBODY></TABLE><BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=750 border=0>
<TBODY>
<TR>
<TD align=middle width=620>
<TABLE cellSpacing=0 cellPadding=0 width=562 border=0>
<TBODY>
<TR>
<TD width=562>
<TABLE cellSpacing=0 cellPadding=0 width=562 border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=1 cellPadding=5 width="100%" border=0>
<TBODY>
<TR>
<TD class=f14 bgColor=#666666><B><FONT color=#ffffff
size=2> <B>相关文章</B></FONT></B></TD></TR>
<TR>
<TD class=f14><A
href="http://fanqiang.chinaunix.net/program/java/2001-05-30/2068.shtml"
target=_blank>jsp入门好文章</A><FONT
color=gray> 2001-05-30 10:08:01</FONT> <BR><A
href="http://fanqiang.chinaunix.net/program/java/2001-05-30/2069.shtml"
target=_blank>JSP白皮书</A><FONT
color=gray> 2001-05-30 11:00:00</FONT> <BR><A
href="http://fanqiang.chinaunix.net/safe/program/2002-01-29/2620.shtml"
target=_blank>JSP应用的安全问题</A><FONT
color=gray> 2002-01-29 20:56:44</FONT> <BR><A
href="http://fanqiang.chinaunix.net/app/other/2001-04-21/2693.shtml"
target=_blank>gnujsp1.0.0在RedHat下基于apache
jserv的安装</A><FONT color=gray> 2001-04-21
18:08:34</FONT><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD height=10></TD></TR></TBODY></TABLE><!--结束:底部-->
<TABLE width=750 border=0>
<TBODY>
<TR>
<TD width="100%" bgColor=#d09f0d colSpan=5 height=2><IMG height=1
src="Taglib原理和实现之循环的Tag - fanqiang_com.files/c.gif" width=1></TD></TR>
<TR>
<TD vAlign=top width="100%" colSpan=5 height=40>
<P align=center><FONT
color=#ffffff>★ 感谢所有的作者为我们学习技术知识提供了一条捷径 ★
<BR>www.fanqiang.com</FONT></P></TD></TR></TBODY></TABLE>
<CENTER></CENTER></TR></TBODY></TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -