站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题JSP标记学习笔记

JSP标记学习笔记

摘要: JSP标记

JSP标记学习笔记<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

ginkou.fly 2002-9-18

1. 概述

1) 什么是JSP标记

顾名思义,JSP标记就是在JSP文件中使用的标记。它类似于html语法中的标记,像head 、table 。通过在JSP文件中引用它(就像使用html标记那样),可以更方便的实现对Java 代码模块的重用。

2) 为什么要使用JSP 标记技术(与javabean相比)

好处:

J JSP 标记可以处理JSP正文的内容,比如改变文本显示样式;而javabean不可以。

J JSP标记接口简单,易于使用,也易于开发和维护。

坏处:

L JSP标记在进行设置时要比javabean复杂很多,体现在JSP标记库描述文件(*.tld)的配置上。

L JSP标记实现的功能远不如javabean强大。

L 当前只要JSP1.1支持JSP标记,而JSP1.0和1.1都支持对Bean的使用。

2. JSP标记的开发简介。

JSP标记的开发需要编写2种独立的文件。一种是定义标记功能的java类文件,一种是将xml元素名称(标记)映射到标记实现的标记库描述符文件。

⑴ 编写标记处理程序类文件

需要继承javax.servlet.jsp.tagext.TagSupport类;

简单示例:

//文件名ExampleTag.java

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.io.*;

public class ExampleTag extends TagSupport {

//程序的执行起始点

public int doStartTag() {

try {

JspWriter out=pageContext.getOut();

out.print(“Custom tag example”);

}catch (IOException ioe) {

System.out.println(“Error in ExampleTag : “ + ioe);

}

return(SKIP_BODY);

//当标记包括的内容为空时返回SKIP_BODY,表示标记功能

//已执行完成

}

}

编译好的class文件将位于服务器的的特定位置。

⑵ 编写标记库描述符文件

将标记名和其代表的功能实现联系在一起。

简单示例:

<? xml version=”1.0” encoding=”ISO-8859-1” ?>

taglib

PUBLIC “-//Sun Microsystem,Inc.//DTD JSP Tag Library 1.1//EN”

http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd>

//以上是标准的文件头

//以下是标签库描述

1.0 //Tag 库的版本

1.1 //Jsp 库的版本

csajsp//自定义标记前缀名

//标记描述符文件的uri,可以是空

My custom tag! //标记库的描述信息

//以下定义新标记

example //自定义的标记名

tags.ExampleTag//定位标签处理类文件

Simplest example//描述信息

<bodycontent>EMPTY</body> // 不处理Jsp文件正文内容

此文件将配置在服务器的特定位置

⑶ 使用以上自定义的标记

HTML PUBLIC “- // W3C//DTD HTML 4.0 Transitional //EN”>

<head>

<%@ taglib uri = “csajsp-taglib.tld” prefix = “csajsp” %> //进行标记使用声明

<csajsp:example /><TITLE></P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"><link> REL=STYLESHEET HREF=”JSP-styles.css”</P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"><SPAN style="mso-spacerun: yes"> TYPE=”text/css”></SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"><head></P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"><body></P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"><H1><B><SPAN style="COLOR: black"><csajsp:example /></B><H1> /</SPAN>使用标记</P> <P class=MsoNormal style="TEXT-INDENT: 42.15pt; mso-char-indent-count: 4.0; mso-char-indent-size: 10.5pt"><B><csajsp:example /><o:p></o:p></B></P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"></body></P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"></HTML></P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt">这样最终在在浏览器上输出为</P> <P class=MsoNormal style="MARGIN-LEFT: 21pt; TEXT-INDENT: 21pt"><SPAN style="mso-spacerun: yes"> Custom tag example</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo1; tab-stops: list 18.0pt">3.<SPAN style="FONT: 7pt 'Times New Roman'"> </SPAN>深入的JSP标记开发</P> <P class=MsoNormal style="TEXT-INDENT: 18pt">⑴ 添加标记属性</P> <P class=MsoNormal style="MARGIN-LEFT: 18pt; TEXT-INDENT: 10.3pt; mso-char-indent-count: .98; mso-char-indent-size: 10.5pt">在标记处理类中可以通过函数setAttribute(String value) 为jsp标记分配属性,这样标记处理程序根据获取的属性值来实现相关功能。</P> <P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt">比如 private String message = “Default Message”;</P> <P class=MsoNormal style="MARGIN-LEFT: 28.45pt; TEXT-INDENT: 21pt">public void setMessage(String message) {</P> <P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN style="mso-spacerun: yes"> this.message=message;</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> }</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 27pt; mso-char-indent-count: 2.99; mso-char-indent-size: 9.0pt"><B>注意</B>:属性名message是小写的,设置函数中的Message是大写的。<o:p></o:p></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt">◆与之相关的在tld文件中需要在tag元素中添加如下元素</P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"><tag></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"><attribute><SPAN style="mso-spacerun: yes"></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"><name>message</name> //属性名<SPAN style="mso-spacerun: yes"></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"><required>false</requied> //属性值是否必须提供。False表示不必,存在缺省值。</P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"><rtexprvalue>true</rtexprvalue></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt">//ture表示属性值可以使用jsp表达式,false表示禁</P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt">//止使用</P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt"></tag></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt">◆对应jsp文件中引用为:<prefix : name message = “ Hello!” /></P> <P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt">⑵ 使用标记正文</P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> </SPAN>即是在标记中使用一些包含jsp脚本元素、文本等的内容,这些内容由jsp引擎处理。</P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> </SPAN>形式如下:</P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> <prefix : tagname> body(</SPAN>正文内容) <prefix : tagname></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> </SPAN>在处理jsp标记的正文前,将会调用处理程序的doStartTag()方法,此时要在此方法中返回EVAL_BODY_INCLUDE 以表明包含有标记正文;处理完后,将会调用doEndTag()方法,此后要让jsp引擎继续处理后面的页面,得返回EVAL_PAGE,否则返回SKIP_PAGE。</P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> </SPAN>◆对应在tld文件中需要在tag元素中添加如下元素</P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> <tag></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> …</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> <bodycontent>JSP</body> //</SPAN>表明包含正文</P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> …</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> </tag></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> </SPAN>另外:可以设置一些条件来判断是否包含正文的,如下:</P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> public int doStartTag() {</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-tab-count: 1"> ServletRequest request = pageContext.getRequest();</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> String debugFlag = request .getParameter(“debug”);</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> If ((debugFlag!=null) && (!debugFlag.equalsIgnoreCase(“false”))) {</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> Return (EVAL_BODY_INCLUDE);</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> }</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> else {</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> return(SKIP_BODY);</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> }</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 0.75pt; mso-char-indent-count: .07; mso-char-indent-size: 10.7pt"><SPAN style="mso-spacerun: yes"> }</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 43.5pt">这样只有在url尾部给出请求参数值debug = true,才会显示标记正文的内容。这样可以隐藏一些调试信息。</P> <P class=MsoNormal style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt">⑶ 对标记正文进行处理</P> <P class=MsoNormal><SPAN style="mso-spacerun: yes"> jsp</SPAN>的标记可以对包含的正文进行处理(修改),然后再输出。这需要标记处理类继承BodyTagSupport类(TagSupport类的继承)。这个类提供了2个重要的方法用于正文处理。</P> <P class=MsoNormal><SPAN style="mso-spacerun: yes"> </SPAN>→ doAfterBody:默认执行的方法,在此方法中包含正文的处理过程。</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">→ getBodyContent:返回BodyContent类型的的对象,此对象包含了有关标记正文的信息。</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> BodyContent</SPAN>类型包含的重要方法:</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">→ getEnclosingWriter:返回JspWriter方法,输出内容。</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">→ getString:返回包含全部jsp标记正文的字符串。</P> <P class=MsoNormal style="TEXT-INDENT: 9.05pt; mso-char-indent-count: 1.0; mso-char-indent-size: 9.05pt"><B>注意</B>:这里和⑵中介绍的输出jsp标记正文的方式不一样。前者的输出是先由jsp引擎解释,这里是在jsp标记处理程序中直接输出到给浏览器。<o:p></o:p></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">另外,在doAferBody中返回SKIP_BODY,表示终止标记正文处理;若返回的是EVAL_BODY_TAG,将会再一次调用doAferBody方法,重新处理标记正文,直到返回SKIP_BODY为止。</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">示例:</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">→标记处理程序片:</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">//下面的程序片将调用一个filter方法,此方法用于把jsp正文中的字符< > “ & 分别用&it;<SPAN style="mso-spacerun: yes"> &gt; &guot; &amp;</SPAN>来代替。以使浏览器按字符原样输出,不进行解释。</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">public class FillterTag extends BodyTagSupport {</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 1"> public int doAfterBody() {</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 1"> BodyContent body=getBodyContent();</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> String filteredBody=ServletUtilities.filter(body.getString()); //</SPAN>将正文得到的字符串过滤</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> Try {</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> JspWriter out=body.getEnclosingWriter();</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 52.5pt; mso-char-indent-count: 5.0; mso-char-indent-size: 10.5pt">out.print(filteredBody); //输出过滤后的文本</P> <P class=MsoNormal style="TEXT-INDENT: 69pt">}catch(IOException ioe) {</P> <P class=MsoNormal style="TEXT-INDENT: 69pt"><SPAN style="mso-spacerun: yes"> System.out.println(“Error in FilterTag: “ + ioe);</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 69pt">}</P> <P class=MsoNormal style="TEXT-INDENT: 69pt">return(SKIP_BODY); //终止jsp正文处理</P> <P class=MsoNormal><SPAN style="mso-tab-count: 2"> }</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt">→标记符描述文件片</P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> …</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> <tag></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> <name>filter</name></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> <tagclass>tags.FilterTag</tagclass></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> <bodycontent>JSP</body></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> </tag></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> …</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt">→jsp文件片</P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-tab-count: 1"> <csajsp:filter></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> <STRONG>I love you!</STRONG><BR></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> <EM>I love you</SPAN>!<EM></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> </csajsp:filter></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> </SPAN>这样,在浏览器中输出的标记正文应当是</P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><STRONG>I love you!</STRONG><BR></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><EM>I love you!<EM></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><SPAN style="mso-spacerun: yes"> </SPAN>而不是</P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><B><SPAN style="mso-spacerun: yes"> I love you!<o:p></o:p></SPAN></B></P> <P class=MsoNormal style="TEXT-INDENT: 32.25pt"><I><SPAN style="mso-spacerun: yes"> I love you!<o:p></o:p></SPAN></I></P> <P class=MsoNormal><I><SPAN style="mso-spacerun: yes"> </SPAN></I> ⑶使用jsp嵌套标记。</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> </SPAN>可以对多个JSP标记进行嵌套引用,这样子标记就可以访问和存储父标记的数据和方法。</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 2"> </SPAN>子标记访问父标记需要使用BodyTagSupport类中的 findAccetorWithClass方法。注意它只能查找临近的父标记。</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> </SPAN>假如在jsp文件中如下的嵌套引用:</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> <!- - </SPAN>此嵌套标记模拟条件判断- -></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> <csajsp:if></SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> <csajsp:condition>true</csajsp:condition></SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> <csajsp:then>Condition was true</csajsp:then></SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> <csajsp:else>Condition was false</csajsp:else></SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> </csajsp:if></SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> </SPAN>相应的标记处理程序片:</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> </SPAN>→对于if标记,执行类如下:</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> public class IfTag extends TagSupport {</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> private boolean condition ;</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> private Boolean hasCondition = flase ;</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 241.5pt; TEXT-INDENT: -241.5pt; mso-char-indent-count: -23.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> public void setCondition(Boolean condition) { //</SPAN>设置判断条件的真假;condition子标</P> <P class=MsoNormal>//记调用</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> this.condition = condition ;</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> hasCondition = true ;</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> }</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: -199.5pt; mso-char-indent-count: -19.0; mso-char-indent-size: 10.5pt">public Boolean getCondition() {<SPAN style="mso-spacerun: yes"> //</SPAN>获取判断条件的真假then 、else子标</P> <P class=MsoNormal>//记调用</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> return(condition) ;</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> }</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> public void setHasCondition(Boolean flag) { //</SPAN>判断if标记后是否存在条件,由</P> <P class=MsoNormal style="TEXT-INDENT: 231pt; mso-char-indent-count: 22.0; mso-char-indent-size: 10.5pt">//condition子标记调用</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> this.hasCondition = flag ;</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> }</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> public Boolean getHasCondition() { //</SPAN>获取是否存在判断条件的信息,由</P> <P class=MsoNormal style="TEXT-INDENT: 231pt; mso-char-indent-count: 22.0; mso-char-indent-size: 10.5pt">//then、else标记调用</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> return(hasCondition) ;</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> }</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> public int doStartTag() {</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> return(EVAL_BODY_INCLUDE) //</SPAN>包含子标记</P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 3"> }</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 2"> }</SPAN></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt"> <o:p></o:p></P> <P class=MsoNormal style="MARGIN-LEFT: 10.5pt; TEXT-INDENT: -10.5pt; mso-char-indent-count: -1.0; mso-char-indent-size: 10.5pt">→那么对于condition等其它子标记的处理程序,需要获取父标记处理程序对象:</P> <P class=MsoNormal>IfTag<SPAN style="mso-spacerun: yes"> parent=(IfTag) findAncestorWithClass(this,IfTag.class)</SPAN></P> <P class=MsoNormal>之后就可以操作父标记处理程序的相应方法。</P> <P class=MsoNormal>示例:(对于condition标记)</P> <P class=MsoNormal>public class IfConditionTag extends BodyTagSupport {</P> <P class=MsoNormal><SPAN style="mso-tab-count: 1"> <B>ifTag parent = (IfTag)findAncestorWithClass(this,IfTag.class); /</B>/</SPAN>获取父标记对应的对象</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">If (parent = = null) {</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 1"> Throw new JspTagException(“condition not inside it”);</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">}</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">return(EVAL_BODY_TAG);</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">}</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"> <o:p></o:p></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">public int doAfterBody() {</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 1"> <B>ifTag parent = (IfTag) findAncestorWithClass(this , IfTag.class) ;<o:p></o:p></B></SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> String bodyString = getBodyContent() . getString() ;</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> If (bodyString.trim().equals(“true”)) {</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 1"> <B>Parent.setCondition(true) /</B>/</SPAN>访问父标记处理类中的方法</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> }else {</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-tab-count: 1"> <B>parent.setCondition(false)</B> ;</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt"><SPAN style="mso-spacerun: yes"> }</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0; mso-char-indent-size: 10.5pt">return(SKIP_BODY) ;</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">}</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">}</P> <P class=MsoNormal style="TEXT-INDENT: 10.5pt; mso-char-indent-count: 1.0; mso-char-indent-size: 10.5pt">其余标记类和此相似。</P> <P class=MsoNormal style="MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo1; tab-stops: list 18.0pt">4.<SPAN style="FONT: 7pt 'Times New Roman'"> </SPAN>小结</P> <P class=MsoNormal style="MARGIN-LEFT: 18pt">对于一些不太复杂和功能单一的逻辑描述,需要传递的参数要求不高时,使用JSP标记,要方便的多。对于大多数的商业逻辑应用,还是使用bean要好的多,也宜于servlet控制。</P> <P class=MsoNormal style="MARGIN-LEFT: 18pt"> <o:p></o:p></P> <P class=MsoNormal>这个东东是俺学习 人邮 出的Servletlet与JSP核心技术》时随手写下的。里面的程序还没具体配置过。过段时间弄了再搞篇实验体会吧。</P> <P class=MsoNormal>有什么值的商讨的发email哦。ginkou@163.com</P></span> <br /> <div style="font-size: 14px; line-height: 25px;"><strong></strong> <br> <a href="/jsp/index.html">↑返回目录</a> <BR> 前一篇: <a href='/jsp/1/4252.html'>Web开发中一种用sql语句完成分页的高效率方法,以jsp/asp为例 </a> <br> 后一篇: <a href='/jsp/1/4254.html'>纠正几个jsp中INCLUDE的错误 </a> </div> <!-- end: 正文 --> <!-- #EndEditable --> <!-- begin: footer --> <div id=footer><center> <a href="http://www.javanb.com/index.html">首页</a> | <a href="http://www.javanb.com/java-sitemap-bd.html">全站 Sitemap</a> | <a href="http://www.javanb.com/contact.html">联系我们</a> | <a href="#" onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.javanb.com/');return(false);">设为首页</a> | <a href="#" onclick="window.external.AddFavorite('http://www.javanb.com/', 'Java 编程资料牛鼻站');return false;">收藏本站</a> <br>版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved <br>   </center></div> <!-- end: footer --> </div> </body><!-- #EndTemplate --></html>