上一篇:Taglib原理和实现 第六章:标签内常用方… >>
Taglib 原理和实现:第四章 循环的T…
显然,这是一个嵌套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
下一篇:Taglib 原理和实现:第三章 tag之间… >>
相关文章:
- · 如何用jsp输出存在于oracle数据库Blob字段中的…
- · 关于JAVA的分页查询操作技术
- · 使用XML封装数据库操作语句的实现(完全版)-…
- · FC3 安装升级手记
- · 网上看到比较方便的vsftpd讲解
- · 子网掩码计算方法
- · 关于pasv模式中,数据端口由谁指定?
- · Vsftpd1.2.0的高级配置篇
- · Vsftpd1.2.0的基础配置篇
- · SERV-U 6002版安全设置全攻略(3)
- · SERV-U 6002版安全设置全攻略(2)
- · SERV-U 6002版安全设置全攻略(1)
- · SERV-U 6002版安全设置全攻略(4)
- · FTP的建立与维护
- · 用SSL加密增强FTP服务器安全性
- · 教你如何巧妙设定匿名FTP的安全
- · 创建IIS6.0下用户隔离模式FTP站点
- · Secure FTP:安全的企业级FTP服务器
- · BulletProof FTP Server:物美价廉
- · ArGoSoft FTP服务器:架设只要一分钟
- · FTP服务器架设--管理篇
- · FTP服务器架设--架设篇
- · FTP服务器架设--安全篇
- · serv-U FTP软件的攻击防守
- · FTP服务器快速架设全功略
- · IIS建立FTP就真的那么弱吗
- · 关于ftp服务程序中不安全因素研究
- · FTP服务器安全
- · FTP服务器端软件Serv-U教程(6)
- · FTP服务器端软件Serv-U教程(2)
- · FTP服务器端软件Serv-U教程(5)
- · FTP服务器端软件Serv-U教程(3)
- · FTP服务器端软件Serv-U教程(1)
- · FTP服务中允许上传权限存在的问题
- · 内网FTP服务器架设不完全解析
- · Linux下FTP的配置与应用
- · wingate FAQ大全
- · SyGate安装前奏
