`
yuanlanjun
  • 浏览: 1181062 次
文章分类
社区版块
存档分类
最新评论

taglib 原理和实现

 
阅读更多
第一章:taglib 原理和实现<wbr><br></wbr>1。问题:Tag究竟是什么?
如何实现一个Tag?
一个tag就是一个普通的java类,它唯一特别之处是它必须继承TagSupport或者BodyTagSupport类。这两个类提供了一些方法,负 责jsp页面和你编写的类之间的交互,例如输入,输出。而这两个类是由jsp容器提供的,无须开发人员自己实现。换句话说,你只需把实现了业务逻辑的类继 承TagSupport或者BodyTagSupport,再做一些特别的工作,你的类就是一个Tag。并且它自己负责和jsp页面的交互,不用你多操 心。
“特别的工作”通常有以下几个步骤:
1)提供属性的set方法,此后这个属性就可以在jsp页面设置。以jstl标 签为例<c:out value=""/>,这个value就是jsp数据到tag之间的入口。所以tag里面必须有一个 setValue方法,具体的属性可以不叫value。例如
setValue(String data){this.data = data;}
这个“value”的名称是在tld里定义的。取什么名字都可以,只需tag里提供相应的set方法即可。
2) 处理 doStartTag 或 doEndTag 。这两个方法是 TagSupport提供的。 还是以<c:out value= ""/>为例,当jsp解析这个标签的时候,在“<”处触发 doStartTag 事件,在“>”时触发 doEndTag 事件。 通常在 doStartTag 里进行逻辑操作,在 doEndTag 里控制输出。
3)编写tld文件。
4)在jsp页面导入tld
这样,你的jsp页面就可以使用自己的tag了。
通常你会发现自己绝大多数活动都集中在 doStartTag 或 doEndTag 方法里。确实如此,熟悉一些接口和类之后,写taglib很容易。正如《jsp设计》的作者所言:里面的逻辑稍微有点复杂,但毕竟没有火箭上天那么难。

2。一个简单的例子:OutputTag

package diegoyun;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author chenys
*/
public class OutputTag extends TagSupport
{
private String name=null;
public void setName(String name)
{
this.name = name;
}

public int doStartTag() throws JspException{
try
{
JspWriter out = pageContext.getOut();
out.print("Hello! " + name);
}
catch (Exception e)
{
throw new JspException(e);
}
return EVAL_PAGE;
}
}
简要说明:
1。如何输出到jsp页面:
调用JspWriter JspWriter out = pageContext.getOut();out.print......
记住这个方法就可以了。
2。输出后如何作处理
函数会返回几个值之一。EVAL_PAGE 表示tag已处理完毕,返回jsp页面。还有几个值,例如 EVAL_BODY_AGAIN 和EVAL_BODY_INCLUDE等,后面我们会作讨论

编写tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>diego</short-name>
<!--OutputTag-->
<tag>
<name>out</name>
<tag-class>diegoyun.OutputTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>

在WEB-INF下新建tlds文件夹,把这个文件取名为diego.tld,放到tlds文件夹下。路径应该这样:WEB-INF/tlds/diego.tld
关于tld的简单说明:
short-name:taglib的名称,也称为前缀。比如<c:out value=""/> 里的“c”
name:tag的名字。例如<c:out value=""/> 里的"out”,我们的类也取名为out,由于有前缀作区分,不会混淆
tag-class:具体的tag类。带包名
body-content:指tag之间的内容。例如<c:out value=""> ......</c> 起始和关闭标签之间就是body-content。由于没有处理body-content,所以上面设为empty
<attribute>里的name:属性名字。例如<c:out value=""/>里的value。名字可任意取,只要类里提供相应的set方法即可。
required:是否必填属性。
rtexprvalue:是否支持运行时表达式取值。这是tag的强大功能。以后我们会讨论。暂时设为false

编写jsp页面
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>
<html>
<body>
Test Tag:
<diego:out name="diegoyun"/>
</body>
</html>

我的编程环境是eclipse+tomcat.启动服务器,如果一切按照上面步骤的话,就能看到 Test Tag: Hello! diegoyun字样

最简单的tag就这么出来了。并不难,是不是?
<wbr>第二章 让Tag支持El表达式</wbr><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">1.先看这么一个例子</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page contentType="text/html; charset=gb2312" language="java"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">String tut = "tutorial";</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">request.setAttribute("tut",tut);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">The String in request is :</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;c:out value="${tut}"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/body&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">2.如何支持el表达式</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">在路径org.apache.taglibs.standard.lang.support下,有个叫ExpressionEvaluatorManager.evaluate 的方法,当el表达式作为入参时,调用这个方法,在tag内即可自动把el 表达式转化。例如,你想tag的value字段支持el表达式,那么只需在set方法里如下调用:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setValue(Object value)throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.value = ExpressionEvaluatorManager.evaluate(</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">"value", value.toString(), Object.class, this, pageContext);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">ExpressionEvaluatorManager.evaluate 有四个参数。第一个表示tag的名字,在取el表达式出错时使用。一般和属性名字相同。第二个要求字符串,通常简单调用输入对象的toString方法。 第三个是类,通常用Object.class。第四个用this即可,第五个是pageContext变量。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">通常不用对这个方法思考太多。只需改改属性名字,其他照搬即可。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">注意:当你的tag属性支持el表达式时,你必须把它声明为Object对象。如上述的value,应该声明为:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object value = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">3.实例:让OutputTag支持El表达式</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspWriter;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.tagext.TagSupport;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class NewOutputTag extends TagSupport</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object name = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setName(Object name) throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.name = ExpressionEvaluatorManager.evaluate(</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">"name", name.toString(), Object.class, this, pageContext);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doStartTag() throws JspException{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">try</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> JspWriter out = pageContext.getOut();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> out.print("Hello! " + name);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">catch (Exception e)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException(e);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_PAGE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"></span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"></span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">在diego.tld里添加声明</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!--NewOutputTag--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;name&gt;newout&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag-class&gt;diegoyun.NewOutputTag&lt;/tag-class&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body-content&gt;empty&lt;/body-content&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;name&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写jsp测试</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page language="java" %&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body bgcolor="#FFFFFF"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">String s = "diego";</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">request.setAttribute("name",s);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Test El supported tag:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;br&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:newout name="${name}"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/body&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">可以看到页面输出为:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Test El supported tag:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Hello! diego</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><wbr>第三章 tag之间的嵌套和属性读取</wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">1。问题:在request里有一个 Man 对象,它有两个属性:name和age。现在,我们想用一个嵌套的tag,父tag取得对象,子tag取得name属性并显示在页面上。例如,它的形式如下:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:with object="${Man}"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:output property="name"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:with&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">object 支持el表达式,表示取得 Man 对象。output的property表示从该对象取得名为name的属性。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">2。如何支持tag之间的嵌套</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">在子tag里调用getParent 方法,可以得到父tag对象。用 findAncestorWithClass 方法,则可以通过递归找到想要找的tag。例如</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:with object="${people}"&gt; &lt;!--表示取得一个对象--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt; diego:withCollection property="men"&gt; &lt;!--表示取得对象里的一个属性,这个属性是个 Collection,Collection里添加了许多man,每个man有名字和年龄--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;diego:output property="name"/&gt;&lt;!--取得name属性并显示--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:withCollection&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:with&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">对于最内层的outputTag来说,调用getParent,可以得到 withCollectionTag,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">通过如findAncestorWithClass(this,WithTag.class)的方式,可以得到withTag</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">得到Tag之后,就可以取得Tag的属性,进行业务逻辑处理,然后输出到jsp</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">3。如何支持类属性查找功能</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">显然,在上面的outputTag中,我们要根据属性的名字,查找类中有没有这个属性。然后取出属性的值并显示。通常,这可以编写自己的反射函数来完成。 更简单的办法,是通过 BeanUtil 的PropertyUtils方法来完成功能。BeanUtil 是apache上的一个开源项目。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">示例如下:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.commons.beanutils.PropertyUtils;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">。。。。。。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">property = PropertyUtils.getProperty(currentClass, propertyName);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">propertyName是待查找属性的名字,例如上面的"name",currentClass是待查找的类,例如上面的People</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">记得把 commons-beanutils.jar添加到WEB-INF/lib目录下</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">4。现在让我们实现开篇提出的问题,编写WithTag如下:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import java.io.IOException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.tagext.BodyTagSupport;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">/**</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">* @author chenys</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">*/</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class WithTag extends BodyTagSupport</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object value = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object output = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setOutput(Object output)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.output = output;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public Object getValue()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return value;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setValue(Object value)throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.value = ExpressionEvaluatorManager.evaluate(</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">"value", value.toString(), Object.class, this, pageContext);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doStartTag()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_BODY_INCLUDE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doEndTag()throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">try</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> pageContext.getOut().print(output);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">catch (IOException e)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException(e);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_PAGE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写 NestedOutputTag 如下:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.tagext.BodyTagSupport;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.commons.beanutils.PropertyUtils;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">/**</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">* @author chenys</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">*/</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class NestedOutputTag extends BodyTagSupport</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private String property = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setProperty(String property)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.property = property;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doEndTag()throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">WithTag parent =(WithTag)getParent();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">if(parent==null)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException("Can not find parent Tag ");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">try</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> Object propertyValue = PropertyUtils.getProperty(parent.getValue(), property);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> parent.setOutput(propertyValue);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">catch (Exception e)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException(e);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_PAGE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">在包diegoyun下添加一个包vo,在vo下写一个Man类:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun.vo;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">/**</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">* @author chenys</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">*/</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class Man</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private String name = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private int age = 0;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int getAge()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return age;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setAge(int age)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.age = age;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public String getName()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return name;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setName(String name)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.name = name;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">写tld</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!--WithTag--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;name&gt;with&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag-class&gt;diegoyun.WithTag&lt;/tag-class&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body-content&gt;JSP&lt;/body-content&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;value&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!--OutputTag3--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;name&gt;nestedout&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag-class&gt;diegoyun.NestedOutputTag&lt;/tag-class&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body-content&gt;empty&lt;/body-content&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;property&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">写jsp页面</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page language="java" %&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page import="diegoyun.vo.*"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body bgcolor="#FFFFFF"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Man man = new Man();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">man.setName("diego");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">request.setAttribute("man",man);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Test nested tag:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;br&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:with value="${man}"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:nestedout property="name"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:with&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/body&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">运行页面,则可以看到:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Test nested tag:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">diego</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">5。结束语:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">上述例子简单描绘了嵌套的Tag之间如何交互。通常子Tag负责取得数据,然后设置父Tag的属性,最后在父Tag里显示到jsp页面。如上面的例子,父Tag 的 output 表示待打印的对象,通过 nestedoutTag 取得name的值,设置output,然后打印出来。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">通过支持El表达式和动态属性联结,Tag可以实现强大的处理功能。将逻辑都集中到Tag里,极大的简化页面的编写。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><wbr>第四章 循环的Tag</wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">1。问题:在request里的 People 对象,有个属性叫 men ,men 是一个Collection ,有许多个man 。现在,把 collection里的man的名字都显示出来</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">显然,这是一个嵌套Tag的问题。有三个Tag互相作用:最外层的Tag找到People对象,中间的Tag取得Collection,子Tag负责打印。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">例如:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:withObject value="${people}"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:withCollection property="men"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;diego:elementout property="name"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:withCollection&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:withObject&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">思路如下:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">1.编写WithObjectTag,负责从El表达式中取得对象</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">2.编写WithCollectionTag,负责从对象中取得 Collection ,遍历 Collection ,每遍历一次 Collection ,执行一次body</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">3.编写ElementoutTag ,把 Collection 中每个men对象的 name 打印出来</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">2. 完整程序如下:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">在上例的diegoyun.vo包内,编写 People 类</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun.vo;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import java.util.Collection;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class People</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Collection men = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public Collection getMen()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return men;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setMen(Collection men)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.men = men;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写 withObject ,这是从request里取得People对象的最外层Tag</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.tagext.BodyTagSupport;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class WithObjectTag extends BodyTagSupport</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object value = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public Object getValue()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return value;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setValue(Object value)throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.value = ExpressionEvaluatorManager.evaluate(</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">"value", value.toString(), Object.class, this, pageContext);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doStartTag()</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_BODY_INCLUDE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doEndTag()throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_PAGE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写WithCollectionTag,该Tag负责取得Collection,并遍历执行子Tag</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import java.util.Collection;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import java.util.Iterator;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.tagext.BodyTagSupport;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.commons.beanutils.PropertyUtils;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class WithCollectionTag extends BodyTagSupport {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object element = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Collection list = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Iterator iterator = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public Object getElement() {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return element;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setProperty(String property) throws JspException {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">//取得父Tag对象,并且得到Collection</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">WithObjectTag parent = (WithObjectTag) getParent();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">if (parent == null)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException("parent tag is null");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">try {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> Object propertyValue = PropertyUtils.getProperty(parent.getValue(),</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> property);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> this.list = (Collection) propertyValue;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> if (list == null)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">throw new JspException("Collection is null");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">} catch (Exception e) {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException(e);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doStartTag() throws JspException {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">//设置第一个元素,然后执行子Tag</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">iterator = list.iterator();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">if (iterator.hasNext())</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> element = iterator.next();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_BODY_INCLUDE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doAfterBody() {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">if (iterator.hasNext()) {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">//如果还存在子元素,设置子元素,并且再次执行子Tag</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">//循环由此而来</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">//否则不再执行子Tag</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> element = iterator.next();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> return EVAL_BODY_AGAIN;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">else</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> return EVAL_PAGE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写 ElementOutputTag</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import java.io.IOException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.tagext.TagSupport;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.commons.beanutils.PropertyUtils;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class ElementOutputTag extends TagSupport</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object propertyValue= null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setProperty(String property)throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">WithCollectionTag parent = (WithCollectionTag)getParent();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">if(parent == null)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException("parent tag is null");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">try</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">//判断上层tag中是否存在该属性名称,如果存在,取得属性值,否则报错</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> propertyValue = PropertyUtils.getProperty(parent.getElement(), property);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">catch (Exception e)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException(e);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doEndTag()throws JspException</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">try</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">//简单的把值打印到jsp页面</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> pageContext.getOut().print(propertyValue);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">catch (IOException e)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException(e);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_PAGE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写tld</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!--WithObjectTag--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;name&gt;withObject&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag-class&gt;diegoyun.WithObjectTag&lt;/tag-class&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body-content&gt;JSP&lt;/body-content&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;value&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!--WithCollectionTag--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;name&gt;withCollection&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag-class&gt;diegoyun.WithCollectionTag&lt;/tag-class&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body-content&gt;JSP&lt;/body-content&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;property&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!--ElementOutputTag--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;name&gt;elementout&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag-class&gt;diegoyun.ElementOutputTag&lt;/tag-class&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body-content&gt;empty&lt;/body-content&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;property&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写jsp</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page language="java" %&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page import="diegoyun.vo.*"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page import="java.util.*"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body bgcolor="#FFFFFF"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Collection c = new ArrayList();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Man man1 = new Man();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">man1.setName("diego");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">c.add(man1);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Man man2 = new Man();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">man2.setName("Zidane");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">c.add(man2);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Man man3 = new Man();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">man3.setName("Rui");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">c.add(man3);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">People p =new People();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">p.setMen(c);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">request.setAttribute("people",p);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Test loop tag:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;br&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:withObject value="${people}"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:withCollection property="men"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:elementout property="name"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;br&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:withCollection&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/diego:withObject&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/body&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">运行,则可以看到:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Test loop tag:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">diego</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Zidane</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Rui</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><wbr>第五章:再论支持El表达式和jstl标签</wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">1。问题:你想和jstl共同工作。比如,在用自己的标签处理一些逻辑之后,让jstl处理余下的工作。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">2。看这个jsp例子:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">....</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">String name="diego";</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">request.setAttribute("name",name);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;c:out value="${name}"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">......</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">许多jstl标签支持El表达式,所以,只要你在自己的标签内部把值塞进request,其他jstl标签就能使用它们</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">3。下面这个例子,从request里面取得对象,找到它属性的值,塞到request里去。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">package diegoyun;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.JspException;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import javax.servlet.jsp.tagext.TagSupport;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.commons.beanutils.PropertyUtils;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public class SetVarTag extends TagSupport</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private Object value = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private String property = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">private String var = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setVar(String var)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.var = var;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setProperty(String property)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">this.property = property;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public void setValue(Object value)throws JspException{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> this.value = ExpressionEvaluatorManager.evaluate(</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">"value", value.toString(), Object.class, this, pageContext);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">public int doEndTag() throws JspException{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Object propertyValue = null;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">try{</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> propertyValue = PropertyUtils.getProperty(value, property);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">catch (Exception e) {</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> throw new JspException(e);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">pageContext.setAttribute(var,propertyValue);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">return EVAL_PAGE;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">}</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写tld</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;!--SetVarTag--&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;name&gt;set&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;tag-class&gt;diegoyun.SetVarTag&lt;/tag-class&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body-content&gt;empty&lt;/body-content&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;value&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;true&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;property&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;name&gt;var&lt;/name&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;required&gt;false&lt;/required&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/attribute&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/tag&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">编写jsp</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page language="java" %&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ page import="diegoyun.vo.*"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c"%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;body bgcolor="#FFFFFF"&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;%</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Man man = new Man();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">man.setName("diego");</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">request.setAttribute("man",man);</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">%&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Get value from request and set it's property value into request:&lt;br&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;diego:set value="${man}" property="name" var="myname"/&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">now use OutTag of jstl taglib to get the name:&lt;br&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">value is : &lt;c:out value="${myname}" /&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/body&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">&lt;/html&gt;</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">运行,效果如下:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">Get value from request and set it's property value into request:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">now use OutTag of jstl taglib to get the name:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">value is : diego</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left">4。结语。和jstl交互是非常有用的技术。在jstl里提供了许多完成基本功能的标签,如输出,循环,条件选择等。仅在处理自己特定逻辑的时候才实现自己的标签,并提供和jstl交互,能大大提高重用性和减少工作量</span><br></wbr></wbr></wbr></wbr>
分享到:
评论

相关推荐

    taglib 原理和实现.

    taglib 原理和实现.taglib 原理和实现.

    Java自定义标签开发.zip

    资源内容:java自定义标签开发;JSP页面中自定义标签详解教程;JSP自定义标签简介;taglib_原理和实现;更多场景的示例Demo;

    自定义标签+struts2标签控制访问链接权限.zip

    资料内容为教程,介绍taglib_原理和实现;JSP自定义标签;JSP页面中自定义标签详解教程;java自定义标签开发;角色标签库;自定义标签帮助文档;自定义标签其它重要资料。

    Struts原理、开发及项目实施

    MVC的工作原理,如下图1所示: &lt;br/&gt; Struts 是MVC的一种实现,它将 Servlet和 JSP 标记(属于 J2EE 规范)用作实现的一部分。Struts继承了MVC的各项特性,并根据J2EE的特点,做了相应的变化与扩展。Struts...

    【计算机软件毕业设计】二手车交易平台的分析、设计与实现文献综述1.doc

    关键词:SSH集成框架 Web 1主流Web开发框架分析 1.1 MVC结构模式和WebWork框架 2012年王欢认为MVC的工作原理是,使用MVC时,当用户向Web容器发送一个请求后, Web容器会根据请求和地址去调用一个Servlet进行处理,...

    类似于QQ的右滑删除效果的实现方法

    原理:删除的div在窗口的外面,用户看不到,用户右滑,显示次div &lt;&#37;@ page language=java contentType=text/html; charset=UTF-8 pageEncoding=UTF-8%&gt; &lt;&#37;@ taglib prefix=c uri=...

    自己动手写搜索引擎(罗刚著).doc

    5.1.3 中文分词的原理 92 5.1.4 查找词典算法 95 5.1.5 最大概率分词方法 98 5.1.6 新词发现 101 5.1.7 隐马尔可夫模型 102 5.2 语法解析树 104 5.3 文档排重 105 5.4 中文关键词提取 106 5.4.1 关键词提取的基本...

    filter使用python3代码进行迭代元素的实例详解

    今天小编为大家带来了新朋友filter函数,相较于以往能实现筛选功能的函数来说是复杂的,这也算是对于一些有难度函数学习的考验。我们会着重于探讨filter函数筛选后的返回值,对于返回值的迭代进行一些原理的分析。 ...

    超级有影响力霸气的Java面试题大全文档

    引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始...

    Java Web编程宝典-十年典藏版.pdf.part2(共2个)

    2.3.3 引用标签库指令taglib的使用 2.4 使用JSP脚本 2.4.1 在JSP中应用代码片段 2.4.2 使用JSP表达式 2.4.3 声明标识的使用 2.5 注释的魅力 2.5.1 基本的HTML注释 2.5.2 JSP隐藏注释 2.5.3 代码片段中的注释 2.6 ...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    它使用服务层框架可以将JavaBeans从Jsp/Servlet中分离出来,而使用表现层框架则可以将Jsp中剩余的JavaBeans完全分离,这部分JavaBeans主要负责显示相关信息,一般是通过标签库(Taglib)实现,不同框架有不同自己的...

    java 面试题 总结

    引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始...

    基于J2EE框架的个人博客系统项目毕业设计论...

    它使用服务层框架可以将JavaBeans从Jsp/Servlet中分离出来,而使用表现层框架则可以将Jsp中剩余的JavaBeans完全分离,这部分JavaBeans主要负责显示相关信息,一般是通过标签库(Taglib)实现,不同框架有不同自己的...

    Java语言基础下载

    Struts framework的工作原理和组件 642 Struts ActionServlet控制器对象 642 Struts Action Classes 642 搞定Action对象 643 处理异常 643 Action的分类 643 Struts Action Mapping 646 使用ActionForward导航 647 ...

    spring security 参考手册中文版

    17.4记住我的接口和实现 138 17.4.1 TokenBasedRememberMeServices 138 17.4.2 PersistentTokenBasedRememberMeServices 139 18.跨站点请求伪造(CSRF) 140 18.1 CSRF攻击 140 18.2同步器令牌模式 141 18.3何时使用...

    Grails 技术精解与Web开发实践【源码+样章】----下载不扣分,回帖加1分,欢迎下载,童叟无欺

    19.2 HibernateCriteriaBuilder的原理 233 19.3 开启Hibernate Query Cache 237 19.4 本章小结 241 第20章 未来Grails版本的新特性 242 20.1 GORM的新特性 242 20.1.1 更多的GORM事件 242 20.1.2 映射基本类型的...

    Java开发技术大全 电子版

    3.4方法的定义和实现116 3.4.1方法的声明117 3.4.2创建方法体与return语句117 3.4.3局部变量和成员变量的区别119 3.4.4方法的访问权限121 3.5方法的调用121 3.5.1方法调用的形式121 3.5.2方法调用的参数123 ...

Global site tag (gtag.js) - Google Analytics