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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值