Chinaunix首页 | 论坛 | 博客
  • 博客访问: 24976
  • 博文数量: 25
  • 博客积分: 1010
  • 博客等级: 少尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-22 20:04
文章分类

全部博文(25)

文章存档

2011年(1)

2009年(24)

我的朋友
最近访客

分类: Java

2009-08-14 17:21:54

定制标签库
 
    一、自定义标签库的一般方法
 
1、html与jsp开发分离技术
 
在传统的JSP程序中,我们将HTML代码与Java代码混合在一起编写,这样虽然方便,但同时也导致页面难以维护,HTML开发人员和JSP开发人员负担加重。
怎样才能做到将HTML开发和JSP开发分离呢?其一是采用JavaBean组件编程技术,另一种形式则是使用标签(Tag)技术。
 
l         在JSP中使用JavaBeans来完成业务功能比直接在JSP页面中插入原始的Java程序要好得多,但是对页面的设计者来说要求熟悉Java程序编程(在JSP页面中对JavaBean中的各个属性和方法进行调用)。
l         通过使用标签技术,我们就可以在页面程序中不出现Java程序代码,并且可以对JavaBean中的各个功能进行标签化,然后页面的设计者通过使用标签来实现对 JavaBean中的后台处理程序进行调用。
 
这样,我们可以让HTML开发人员专注于页面的外观,而Java程序员则不用理会页面显示,专注于后台程序,大大提高了程序的可维护性和方便性。便于各程序员之间的协作开发。也即JSP程序员可以建立定制的标签和后端代码模块,而Web设计人员则可以使用定制标签并且全力关注于Web设计本身。
 
2、什么是标签库? 
 
JSP标签库(也被称作定制标签)是一种通过JavaBean生成基于XML的脚本的方法。从概念上讲,标签就是很简单而且可重用的代码结构。
定制标签使得JSP项目中很容易创建重用的开放源代码模块。而你的全部需要不过就是标签库及其文档。
通过标记库可以定义出现在JSP页面中的作为类XML元素的自定义标签,可以把特定的Java代码与每个标记都关联起来。 例如,假定你可以访问一个天气情况数据库,而且你需要输出现在的天气状况。 那么,你可以在JSP中插入JDBC程序代码直接查询数据库(虽然这并不是一个好的选择),把这些代码封装到一个JavaBean内,或者把它包装成一个标记库。
 
使用标记库时的JSP页面内的程序代码看上去就像:
      
       <%@taglib uri="the TLD file" prefix="foo" %>
       Current weather is <foo:Weather/>
 
注意在上述程序代码中看不到任何Java代码的踪迹。 作为一个页面设计人员,你使用一种熟悉的语法就像<foo:weather/>,这些与其它的任何标记看起来非常相似。 在页面中包含现在的天气状况的HTML字符串的地方插入它。
 
3、标签库的重要特性
 
l         易于安装在多个项目上
标签很容易从一个JSP项目迁移到其他项目。一旦建立了一个标签库,你只需要把这个标签库包装成一个JAR文件就可以在其他JSP项目中重新使用了。不能重用的是你作为程序员在建立标签时所加进标签的内容。因为标签可以重新使用,所以标签库可以轻松地用于你自己的项目。目前,最好的标签资源可以在JSPTags.com这个站点找到。.
l         扩展JSP
标签库可以具备JSP规范(JSP 1.2)中的任何特性和功能。这也意味着你拥有了无限的能力可以扩展和增加JSP的强大功能却无需等待新版本JSP的发布。所以说,你完全可以取消页面上的JSP include调用了——只需用include标签建立自己的规范就可以了。
l         易于维护
标签库使得JSP Web应用程序变得很容易维护
例如:你可能有如下的一个标签,它让你的文本都显示蓝色:
My Text
但是随着项目的进行,你又想让蓝色变暗。于是你保留自己的标签但是为其增加了一个新的属性:shade
My Text
    所有的老标签继续显示蓝色,但现在你可以使用同一标签来产生变暗的蓝色文本了。
 
4、使用标签库taglib技术简化程序设计
 
、等均为HTML中的标签,而是一个标签库中的某个标签,其中Jsp为该标签库的库名,forward为标签名称。
在JSP中,一个标签看起来很象一个标准的HTML标签。但是,它的逻辑不在客户端被执行,而是由服务器端的Servlet来转换。每个标签被封装到一个独立的类,它的名字和参数属性被一个特殊的.tld扩展名的部署描叙文件中来指出。
通过使用标签库技术也能使网页设计师不用编程JSP的Java代码就操作复杂的数据库和事务等。特别适用于需要重复使用某一段程序的地方。
 
5、定制标签的编程实现步骤:
 
l         JavaBean:为了充分利用Java的面向对象特性,重用代码应当放置在独立的代码容器内。这些JavaBean可不是标签库的一部分。它们是标签库用来执行所分配任务的基础代码模块(可以将这些*.class文件发布为JAR文件)。
l         标签库描述符(TLD文件):这是一种很简单的XML文件,TLD文件描述和说明了属性、信息和标签处理器文件位置等信息。JSP 容器使用这一文件来映射被调用标签库的位置和用法。
l         Web站点的web.xml文件:其实这就是你的Web站点上的初始化文件。在这个文件内你可以定义Web应用程序中使用的定制标签,还可以定义用来描述每个定制标签的TLD文件。
l         JSP页面上的标签库声明:仅需使用标签库标示符即可方便地声明页面上存在的标签。标签库在页面上声明之后,你就可以随意使用它们了。
 
6不带属性和标签体的简单标签的开发例
 
JSP标签的开发需要编写两种独立的文件。一种是定义标签功能的java类文件,一种是将xml元素名称(标签)映射到标签实现的标签库描述符文件。
 
(1)编写标签处理程序的JAVA类文件( TAG处理器)
 
当一个JSP引擎遇到一个定制的标签时,它首先检查是否能够确定这个标签类的物理位置。如果能够确定,就会执行标签类中对应的代码。
该标签类的Java文件执行由标签所标示的具体工作,并应该被放置在WEB-INF下的classes目录或者其下的某一子目录(取决于是否有包的定义)下。标签类通常页可以被制作成一个JAR文件,放置在WEB-INF目录下的lib目录中。
需要在tld标签库描述文件里的每个TAG标签中指明是应用哪一个类文件来对这个TAG进行处理。
 
/*
在JCreator中编译该程序时要注意:由于在JDK.14中
并没有包含javax.servlet.jsp.*、javax.servlet.jsp.tagext.*等包,
应该在JCreator中引入这些包,这些包可以从
Tomcat\common\lib\jsp-api.jar和servlet-api.jar文件
*/
package jspTagLib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;  
 
public class JSPTagTest extends TagSupport
{
    public int doStartTag()     //程序的执行起始点
    {
        try
        {
/*
    out是一个JspWriter类型的隐含对象,
用于向jsp网页输出内容。输出的目标由pageContext.getout提供 ,
其中pageContext是TagSupport类中的一个PageContext类的对象
*/         
            JspWriter out=pageContext.getOut();
            //向jsp网页上下文输出
            out.println("这是定制JSP标签所输出的文字");            }
        catch(Exception e)
        {
                System.out.println("tag err!");
        }
     //由于没有标签体,因此返回SKIP_BODY,表示标签功能已执行完成
    return(this.SKIP_BODY);
    }      
}
编程要点:
 
(1)需要继承javax.servlet.jsp.tagext.TagSupport类并重写doStartTag方法。JSP服务器引擎当它第一次遇到这个标记的时候,引擎就会调用doStartTag ()方法。
l         可以使这个方法为空或者当需要的时候才执行应用程序逻辑。
l         当该方法返回SKIP_BODY时,那么引擎跳过这个标记体。
l         当它返回EVAL_BODY_INCLUDE时,引擎将会处理这个标记以及它的子标记。
 
(2)在JCreator中编译该程序时要注意:由于在JDK.14中并没有包含javax.servlet.jsp.*、javax.servlet.jsp.tagext.*等包,应该在JCreator中引入这些包,这些包可以从Tomcat\common\lib\jsp-api.jar和servlet-api.jar文件获得
 
(3)将编译好的class文件放于服务器的WEB-INF下的classes目录下(由于该Java程序有包的定义,因此应该将它的*.class文件放在WEB-INF\classes\jspTagLib目录下,其中jspTagLib为包名)。
注意:
l         只需要将该JSPTagTest.java文件放在WEB-INF\classes目录下,然后对它进行编译将自动会形成上述目录位置。
l         也可以将标签类的文件制作成一个JAR文件,放置在WEB-INF目录下的lib目录中。
 
(4)编写标签库描述符TLD文件(Tag Library Descriptor,文件扩展名为*.tld)
 
通过该文件实现将标签名和其代表的功能实现联系在一起,它规定了标签和Java程序类的映射关系,由容器解释;它是标准的XML格式的标记定义文件,可以定义标签和它的各种属性(包括它的名称,版本和其它的任选信息)和处理文件等等;
文件名:目录WEB-INF/taglib
JSPTageTest.tld
 
    "">

标签库的版本
   

      1.0

这个标签库要求的JSP版本
      1.1                     

 

自定义标签前缀名
      userTag                       

          

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

 
      标签库的使用描述信息                        

标签库的使用描述信息
自定义的标签
 


                                      
 

处理这个标签的java类的名字可以(包含有包的名称),不同的标签可能对应不同的java类来处理
自定义的标签名
        JSPTagTest                     

 
         jspTagLib.JSPTagTest   
 
        标签的描述信息                

标签的描述信息
     

   
 
注意:只有黑体的两句最重要,其他就照抄;该文件需要放置在WEB-INF目录或其子目录下。
对WebLogic也是一样的要求

相对于/WEB-INF目录
 


(5)修改WEB-INF目录下的web.xml文件以定位标签的*.tld文件
 
在web-app之间添加如下的项目:
 
    /taglib/JSPTagTest.tld       
   
        /WEB-INF/taglib/JSPTagTest.tld       

自定义的标签库名的前缀,请不要用jsp, jspx, java, javax, servlet, sun, 和sunw做为你的前缀
进行标记使用声明,声明此JSP文件使用了自定义的标签,同时引用标签库,也指定了他们的标签的前缀。
   

 
 
(6)在JSP页面中使用以上自定义的标记
<%@ taglib uri="/taglib/JSPTagTest.tld" prefix="userTag" %>

应该与前面*.tld文件中的name声明的标签名保持相同

</FONT></DIV> <DIV><FONT size=3><userTag:JSPTagTest />  </FONT></DIV> <P> <TABLE cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD> <DIV> <DIV><FONT size=3>应该与上面声明的自定义的标签库名保持相同</FONT></DIV></DIV></TD></TR></TBODY></TABLE><FONT size=3><TITLE></FONT></P> <DIV><FONT size=3><HEAD><BODY></FONT></DIV> <DIV><FONT size=3><H1></FONT></DIV> <DIV><FONT size=3><userTag:JSPTagTest /></FONT></DIV> <DIV><FONT size=3></H1> </FONT></DIV> <DIV><FONT size=3><script type="text/javascript" src="/js/jquery.qqFace.js"></script> </BODY></HTML><B></B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>7</B><B>、标签转换机制</B></FONT></DIV> <DIV><FONT size=3>       </FONT></DIV> <DIV><FONT size=3>首先调用JSP引擎把JSP页面翻译成一个servlet,然后再编译servlet。 与标记库相关联的方法都被包含在servlet中。</FONT></DIV> <DIV><FONT size=3></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>8</B><B>、</B><B>为什么要使用JSP 标签技术(与JavaBean相比)</B></FONT></DIV> <DIV><FONT size=3>    </FONT></DIV> <DIV><FONT size=3>JSP 1.1提出了一种扩展的JSP标签的方法,这种方法命名为“标签库”。JSP标签从本质上代表着一个JSP功能的调用,即一个标签触发一次函数调用。同时标签也具有JavaBean所具有的业务逻辑处理能力。</FONT></DIV> <DIV><B><FONT size=3>(1)优点:</FONT></B></DIV> <DIV><FONT size=3>JSP 标签可以处理JSP正文的内容,比如改变文本显示样式,而JavaBean不可以;</FONT></DIV> <DIV><FONT size=3>JSP标签接口简单,易于使用,也易于开发和维护。</FONT></DIV> <DIV><B><FONT size=3>(2)缺点:</FONT></B></DIV> <DIV><FONT size=3>JSP标签在进行设置时要比JavaBean复杂很多,体现在JSP标签库描述文件(*.tld)的配置上;</FONT></DIV> <DIV><FONT size=3>JSP标签实现远不如JavaBean方便。</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>9</B><B>、标签的类型</B><B></B></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3>(1)不带属性和标签体的简单标签 <mytaglibs: SomeTag /></FONT></DIV> <DIV><FONT size=3>(2)不带标签体但有属性的标签 <mytaglibs: SomeTag  user=”UserName”/></FONT></DIV> <DIV><FONT size=3>(3)带标签体同时也有属性的标签 </FONT></DIV> <DIV><FONT size=3>    <mytaglibs: SomeTag  user=”UserName”></FONT></DIV> <DIV><FONT size=3>       此内容为该标签的标签体 </FONT></DIV> <DIV><FONT size=3>    </mytaglibs: SomeTag><B></B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>10</B><B>、TagSupport和BodyTagSupport类</B><B></B></FONT></DIV> <DIV><B><FONT size=3>       </FONT></B></DIV> <DIV><FONT size=3>在编程定义标签功能的java类文件时,可以采用实现Tag接口或者继承TagSupport类的方法,但一般都采用继承TagSupport类的方法。</FONT></DIV> <DIV><FONT size=3>    但对<B>带标签体的标签,</B>在编程定义该类型的标签功能的java类文件时,可以采用实现BodyTag接口或者继承BodyTagSupport类的方法,但一般都采用继承BodyTagSupport类的方法。</FONT></DIV> <DIV><FONT size=3>两者的主要区别是标签处理类是否需要与标签体交互(也即标签处理类是否要读取标签体的内容和改变标签体返回的内容),如果不需要交互的应用就用TagSupport,否则就用BodyTagSupport。</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>二、带属性的JSP</B><B>标签</B><B>的开发例</B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(1)编程要点</FONT></B></DIV> <DIV><FONT size=3>一个标签能接受输入参数,只需要在该标签所对应的Java类程序中增加setXXX()方法即可,其中xxx为该标签的属性名称 。</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(2)编写带属性的</B><B>标签</B><B>处理程序的JAVA类文件</B></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3>package jspTagLib;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.*;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.tagext.*;</FONT></DIV> <DIV><FONT size=3>import java.io.*;   </FONT></DIV> <DIV><FONT size=3>public class JSPTagTest extends TagSupport</FONT></DIV> <DIV><FONT size=3>{</FONT></DIV> <DIV><FONT size=3>    private String tagproperty="属性的缺省值";  // tagproperty为该标签的属性名称</FONT></DIV> <DIV><FONT size=3>    public int doStartTag()     //程序的执行起始点</FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        try</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>    out是一个JspWriter类型的对象,用于向jsp网页输出内容。输出的目标由pageContext.getOut提供 ,其中pageContext是TagSupport类中的一个PageContext类的对象</FONT></DIV> <DIV><FONT size=3>*/          </FONT></DIV> <DIV><FONT size=3>            JspWriter out=pageContext.getOut();</FONT></DIV> <DIV><FONT size=3>            if(tagproperty.equals("属性的缺省值"))</FONT></DIV> <DIV><FONT size=3>            {</FONT></DIV> <DIV><FONT size=3>                out.println("这是定制JSP标签所输出的文字并且没有设置属性");        </FONT></DIV> <DIV><FONT size=3>            }</FONT></DIV> <DIV><FONT size=3>            else</FONT></DIV> <DIV><FONT size=3>            {</FONT></DIV> <DIV><FONT size=3>out.println("这是定制带属性的JSP标签所输出的文字,您所设置的tagproperty属性的值为:"+tagproperty);    </FONT></DIV> <DIV><FONT size=3>            }</FONT></DIV> <DIV><FONT size=3>//向jsp网页上下文输出,在JspWriter类中的println()方法与PrintWriter类中的println()不同,它有IOException异常声明</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        catch(IOException  e)</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>            System.out.println(e.getMessage());</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        return this.SKIP_BODY; //当标签包括的内容为空时返回SKIP_BODY,表示标签功能已执行完成</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>重写doEndTag()方法后,该标签可以以如下两种方式来使用(否则只能以<mytaglibs: SomeTag />方式来使用)</FONT></DIV> <DIV><FONT size=3>    <mytaglibs: SomeTag />   或者</FONT></DIV> <DIV><FONT size=3>    <mytaglibs: SomeTag ></mytaglibs: SomeTag ></FONT></DIV> <DIV><FONT size=3>*/</FONT></DIV> <DIV><FONT size=3>    public int doEndTag()  throws JspException      </FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        return(EVAL_PAGE);</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3>    <B>public void setTagproperty(String newTagproperty)</B></FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        this.tagproperty=newTagproperty;</FONT></DIV> <DIV><FONT size=3>    }       </FONT></DIV> <DIV><FONT size=3>}</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(3)修改</B><B>标签</B><B>库描述符TLD文件(仍然采用前面示例中的TLD文件)</B></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3>在tag之间加入<attribute>标签</FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3><?xml version="1.0" encoding="ISO-8859-1" ?></FONT></DIV> <DIV><FONT size=3><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"</FONT></DIV> <DIV><FONT size=3>    ""></FONT></DIV> <DIV><FONT size=3>    <taglib></FONT></DIV> <DIV><FONT size=3>      <tlibversion>1.0</tlibversion></FONT></DIV> <DIV><FONT size=3>      <jspversion>1.1</jspversion>                      </FONT></DIV> <DIV><FONT size=3>      <shortname>userTag</shortname>                        </FONT></DIV> <DIV><FONT size=3>      <uri></uri> </FONT></DIV> <DIV><FONT size=3>      <info>标签库的使用描述信息</info>                         </FONT></DIV> <P> <TABLE cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD> <DIV> <DIV><FONT size=3>表示该标签没有包含标签体内容</FONT></DIV></DIV></TD></TR></TBODY></TABLE><FONT size=3> </FONT></P><BR clear=all> <DIV><FONT size=3>      <tag>                                 </FONT></DIV> <DIV><FONT size=3>        <name> JSPTagTest </name>                       </FONT></DIV> <DIV><FONT size=3>        <tagclass>jspTagLib.JSPTagTest </tagclass>  </FONT></DIV> <P> <TABLE cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD> <DIV> <DIV><FONT size=3>定义属性的名称,必须与*.java中的String tagproperty保持相同</FONT></DIV></DIV></TD></TR></TBODY></TABLE><FONT size=3>        <info>标签的描述信息</info></FONT></P> <DIV><FONT size=3>        <bodycontent> empty </bodycontent>  </FONT></DIV> <DIV><FONT size=3>        </FONT></DIV> <P> <TABLE cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD> <DIV> <DIV><FONT size=3><required>说明该属性是否必须出现在标记中,此属性为可选的并且有缺省值</FONT></DIV></DIV></TD></TR></TBODY></TABLE><FONT size=3>        <B><attribute></B></FONT></P> <DIV><B><FONT size=3>           <name>tagproperty</name></FONT></B></DIV> <DIV><B><FONT size=3>           <required>false</required></FONT></B></DIV> <DIV><B><FONT size=3>           <rtexprvalue>true</rtexprvalue></FONT></B></DIV> <DIV><FONT size=3><B>        </attribute></B>              </FONT></DIV> <DIV><FONT size=3>      </tag> </FONT></DIV> <P> <TABLE cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD> <DIV> <DIV><FONT size=3><rtexprvalue>说明是否该属性可以采用<%=expression%>JSP表达式来赋值</FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">//</FONT>止使用</FONT></DIV> <DIV><FONT size=3>使用</FONT></DIV></DIV></TD></TR></TBODY></TABLE><FONT size=3>    </taglib></FONT></P> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><B><FONT size=3>(4)对WEB-INF目录下的web.xml文件仍然采用前面例中的web.xml文件</FONT></B></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(5)在JSP页面中使用该标签</FONT></B></DIV> <DIV><FONT size=3><%@ taglib uri="<B>/taglib/JSPTagTest.tld</B>" prefix="userTag" %></FONT></DIV> <DIV><FONT size=3><!DOCTYPE HTML PUBLIC  “- // W3C//DTD HTML 4.0 Transitional //EN”></FONT></DIV> <DIV><FONT size=3><HTML><HEAD></FONT></DIV> <DIV><FONT size=3><TITLE></FONT></DIV> <DIV><FONT size=3><userTag:JSPTagTest tagproperty="属性的值"/></FONT></DIV> <P> <TABLE cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD> <DIV> <DIV><FONT size=3>以请求参数作为属性的值</FONT></DIV></DIV></TD></TR></TBODY></TABLE><FONT size=3><TITLE></FONT></P> <DIV><FONT size=3><HEAD><BODY></FONT></DIV> <DIV><FONT size=3><H1></FONT></DIV> <DIV><FONT size=3><userTag:JSPTagTest tagproperty=</FONT>"<%=request.getParameter(\"formItemName\")%>"> <FONT size=3>/></FONT></DIV> <DIV><FONT size=3></H1> </FONT></DIV> <DIV><FONT size=3></BODY></HTML></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>三、设计采用成对的标签例(如:<userTag:JSPTagTest >这是成对的标签正文内容</userTag:JSPTagTest>)</FONT></B></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(1)编程要点</FONT></B></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3>l</FONT>         <FONT size=3>此时应该在标记Java程序类中同时重载doStartTag()和doEndTag()方法,上面的两个标签分别调用这两个方法。</FONT></DIV> <DIV><FONT size=3>l</FONT>         <FONT size=3>而如果要求将“这是成对的标签正文内容”字符串原样输出,则应该注意的是在doStartTag()中需要返回return(EVAL_BODY_INCLUDE)表示要求计算标签体正文内容,而不是return(SKIP_BODY)表示跳过标签体正文内容</FONT></DIV> <DIV><FONT size=3>l</FONT>         <FONT size=3>而doEndTag()需要return(EVAL_PAGE)表示要求容器在标签结束时继续计算JSP页面其他的部分。<B></B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(2)编写带属性的</B><B>标签</B><B>处理程序的JAVA类文件</B></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV align=left><FONT size=3>在JCreator中编译该程序时要注意:由于在JDK.14中并没有包含javax.servlet.jsp.*、javax.servlet.jsp.tagext.*等包,应该在JCreator中引入这些包,这些包可以从Tomcat\common\lib\jsp-api.jar和servlet-api.jar文件</FONT></DIV> <DIV><FONT size=3>*/</FONT></DIV> <DIV><FONT size=3>package jspTagLib;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.*;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.tagext.*;</FONT></DIV> <DIV><FONT size=3>import java.io.*;   </FONT></DIV> <DIV><FONT size=3>public class JSPTagTest extends TagSupport</FONT></DIV> <DIV><FONT size=3>{</FONT></DIV> <DIV><FONT size=3>    private String tagproperty="属性的缺省值";</FONT></DIV> <DIV><FONT size=3>    public int doStartTag()     //程序的执行起始点</FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        try</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>    out是一个JspWriter类型的隐含对象,用于向jsp网页输出内容。输出的目标由pageContext.getOut提供 ,其中pageContext是TagSupport类中的一个PageContext类的对象</FONT></DIV> <DIV><FONT size=3>*/          </FONT></DIV> <DIV><FONT size=3>            JspWriter out=pageContext.getOut();</FONT></DIV> <DIV><FONT size=3>            if(tagproperty.equals("属性的缺省值"))</FONT></DIV> <DIV><FONT size=3>            {</FONT></DIV> <DIV><FONT size=3>                out.println("这是定制JSP标签所输出的文字并且没有设置属性");        </FONT></DIV> <DIV><FONT size=3>            }</FONT></DIV> <DIV><FONT size=3>            else</FONT></DIV> <DIV><FONT size=3>            {</FONT></DIV> <DIV><FONT size=3>out.println("这是定制带属性的JSP标签所输出的文字,您所设置的tagproperty属性的值为:"+tagproperty);    </FONT></DIV> <DIV><FONT size=3>            }</FONT></DIV> <DIV><FONT size=3>//向jsp网页上下文输出,在JspWriter类中的println()方法与PrintWriter类中的println()不同,它有IOException异常声明</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        catch(IOException  e)</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>            System.out.println(e.getMessage());</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        return this.EVAL_BODY_INCLUDE;      //当标签包括有标签体内容时不能再返回SKIP_BODY</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3><B>    public int doEndTag()  throws JspException    //</B><B>结束标签</B></FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        return(EVAL_PAGE);</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3>    public void setTagproperty(String newTagproperty)</FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        this.tagproperty=newTagproperty;</FONT></DIV> <DIV><FONT size=3>    }       </FONT></DIV> <DIV><FONT size=3>}</FONT></DIV> <DIV><FONT size=3>注意:JSP引擎在分析了结束标记之后就会调用doEndTag ()方法。</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(3)修改*.tld文件(仍然采用前面示例中的TLD文件),所应该注意的是要在<tag>标签之间加“<bodycontent>JSP</bodycontent>”标签,表示标签有标签体内容</FONT></B></DIV> <DIV><FONT size=3>      <tag>                                 </FONT></DIV> <DIV><FONT size=3>        <name> JSPTagTest </name>                       </FONT></DIV> <DIV><FONT size=3>        <tagclass>jspTagLib.JSPTagTest </tagclass>  </FONT></DIV> <DIV><FONT size=3>        <info>标签的描述信息</info></FONT></DIV> <DIV><FONT size=3><!--        </FONT></DIV> <DIV><FONT size=3>        <bodycontent> empty </bodycontent>  </FONT></DIV> <DIV><FONT size=3>-->     </FONT></DIV> <DIV><FONT size=3>        <B><bodycontent>JSP</bodycontent></B></FONT></DIV> <DIV><FONT size=3>        <attribute></FONT></DIV> <DIV><FONT size=3>            <name>tagproperty</name></FONT></DIV> <DIV><FONT size=3>            <required>false</required></FONT></DIV> <DIV><FONT size=3>            <rtexprvalue>true</rtexprvalue></FONT></DIV> <DIV><FONT size=3>        </attribute>                </FONT></DIV> <DIV><FONT size=3></tag></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(4)对WEB-INF目录下的web.xml文件仍然采用前面例中的web.xml文件</FONT></B></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(5)在JSP页面中使用该标签</B></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3><%@ taglib uri="<B>/taglib/JSPTagTest.tld</B>" prefix="userTag" %></FONT></DIV> <DIV><FONT size=3><!DOCTYPE HTML PUBLIC  “- // W3C//DTD HTML 4.0 Transitional //EN”></FONT></DIV> <DIV><FONT size=3><HTML><HEAD></FONT></DIV> <DIV><FONT size=3><TITLE></FONT></DIV> <DIV><FONT size=3><B><userTag:JSPTagTest ></B><B>这是成对的标签正文内容</userTag:JSPTagTest></B></FONT></DIV> <DIV><FONT size=3><TITLE></FONT></DIV> <DIV><FONT size=3><HEAD><BODY> </FONT></DIV> <DIV><FONT size=3></BODY></HTML></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>四、如果需要处理标签对间的信息---带标签体的标签并且对标签体进行处理例</B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(1)编程要点:从继承BodyTagSupport类,并重载doAfterBody()方法。</FONT></B></DIV> <DIV><FONT size=3><B>(2)编写该类型的</B><B>标签</B><B>处理程序的JAVA类文件,可以将标签类继承BodyTagSupport类,并重载doAfterBody()方法。</B></FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>在JCreator中编译该程序时要注意:由于在JDK.14中并没有包含javax.servlet.jsp.*、javax.servlet.jsp.tagext.*等包,应该在JCreator中引入这些包,这些包可以从Tomcat\common\lib\jsp-api.jar和servlet-api.jar文件</FONT></DIV> <DIV><FONT size=3>*/</FONT></DIV> <DIV><FONT size=3>package jspTagLib;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.*;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.tagext.*;</FONT></DIV> <DIV><FONT size=3>import java.io.*;   </FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>    继承BodyTagSupport类的主要目的是能够处理标签的正文内容,否则应该继承TagSupport类</FONT></DIV> <DIV><FONT size=3>*/  </FONT></DIV> <DIV><FONT size=3>public class JSPTagTest extends BodyTagSupport  </FONT></DIV> <DIV><FONT size=3>{</FONT></DIV> <DIV><FONT size=3>    private String tagproperty="属性的缺省值";</FONT></DIV> <DIV><FONT size=3>    public int doStartTag()     //程序的执行起始点</FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        try</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>    out是一个JspWriter类型的隐含对象,用于向jsp网页输出内容。输出的目标由pagecontext.getout提供 ,其中pageContext是TagSupport类中的一个PageContext类的对象</FONT></DIV> <DIV><FONT size=3>*/          </FONT></DIV> <DIV><FONT size=3>            JspWriter out=pageContext.getOut();</FONT></DIV> <DIV><FONT size=3>            if(tagproperty.equals("属性的缺省值"))</FONT></DIV> <DIV><FONT size=3>            {</FONT></DIV> <DIV><FONT size=3>                out.println("这是定制JSP标签所输出的文字并且没有设置属性");        </FONT></DIV> <DIV><FONT size=3>            }</FONT></DIV> <DIV><FONT size=3>            else</FONT></DIV> <DIV><FONT size=3>            {</FONT></DIV> <DIV><FONT size=3>                out.println("这是定制带属性的JSP标签所输出的文字,您所设置的tagproperty属性的值为:"+tagproperty);     </FONT></DIV> <DIV><FONT size=3>            }</FONT></DIV> <DIV><FONT size=3>//向jsp网页上下文输出,在JspWriter类中的println()方法与PrintWriter类中的println()不同,它有IOException异常声明</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        catch(IOException  e)</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>            System.out.println(e.getMessage());</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        return this.EVAL_BODY_INCLUDE;</FONT></DIV> <DIV><FONT size=3>//当标签包括有标签体的内容时不能再返回SKIP_BODY, 表示要求计算标签体正文内容</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>    该方法主要是为了能够处理结束标签的</FONT></DIV> <DIV><FONT size=3>*/  </FONT></DIV> <DIV><FONT size=3>    public int doEndTag()  throws JspException</FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        return(EVAL_PAGE);  //表示要求容器在标签结束时继续计算JSP页面其他的部分</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>    该方法主要是为了给标签提供属性项目的</FONT></DIV> <DIV><FONT size=3>*/  </FONT></DIV> <DIV><FONT size=3>    public void setTagproperty(String newTagproperty)           </FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        this.tagproperty=newTagproperty;</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3>/*</FONT></DIV> <DIV><FONT size=3>    该方法主要是为了处理标签之间的标签体正文内容的,该方法是默认执行的方法,在此方法中包含对正文的处理过程与逻辑</FONT></DIV> <DIV><FONT size=3>*/  </FONT></DIV> <DIV><FONT size=3>    public int doAfterBody()</FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        BodyContent tagContentText=getBodyContent();    //获得标签之间的正文内容</FONT></DIV> <DIV><FONT size=3>        String contentString=tagContentText.getString(); //返回包含jsp标签正文的全部字符串</FONT></DIV> <DIV><FONT size=3>        try</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>            JspWriter out=pageContext.getOut();</FONT></DIV> <DIV><FONT size=3>            out.println(contentString);                 //将标签之间的正文内容在页面中输出</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        catch(Exception e)</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3>//  在doAferBody中返回SKIP_BODY,表示终止标签体正文处理;若返回的是EVAL_BODY_TAG,将会再一次调用doAferBody方法,重新处理标记正文,直到返回SKIP_BODY为止。</FONT></DIV> <DIV><FONT size=3>        return this.SKIP_BODY;</FONT></DIV> <DIV><FONT size=3>    }       </FONT></DIV> <DIV><FONT size=3>}</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(</B><B>3</B><B>)注意:</B>doAfterBody ()方法是在JSP引擎处理标签体正文之后被执行,它在doEndTag ()方法之前被调用。</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>五、访问数据库的</B><B>标签设计例</B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(1)</B><B>标签处理程序,请见</B><B>GetDBDataTag.java</B><B>文件。在该程序中实现数据库的连接和操作并将所操作的结果直接输出到页面上,实现某一独立的功能。</B></FONT></DIV> <DIV><FONT size=3>package jspTagLib;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.*;</FONT></DIV> <DIV><FONT size=3>import javax.servlet.jsp.tagext.*;</FONT></DIV> <DIV><FONT size=3>import java.sql.*;</FONT></DIV> <DIV><FONT size=3>public class GetDBDataTag extends TagSupport</FONT></DIV> <DIV><FONT size=3>{</FONT></DIV> <DIV><FONT size=3>     </FONT></DIV> <DIV><FONT size=3>   Connection con = null;</FONT></DIV> <DIV><FONT size=3>   Statement state = null;</FONT></DIV> <DIV><FONT size=3>   ResultSet rs = null;</FONT></DIV> <DIV><FONT size=3>   </FONT></DIV> <DIV><FONT size=3>   String url="jdbc:odbc:WebMisDB";</FONT></DIV> <DIV><FONT size=3>   String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";      </FONT></DIV> <DIV><FONT size=3>   </FONT></DIV> <DIV><FONT size=3>   public GetDBDataTag()throws ClassNotFoundException</FONT></DIV> <DIV><FONT size=3>   {</FONT></DIV> <DIV><FONT size=3>      Class.forName(DBDriver);</FONT></DIV> <DIV><FONT size=3>   }     </FONT></DIV> <DIV><FONT size=3>    public int doStartTag() throws JspTagException </FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>        return EVAL_BODY_INCLUDE;</FONT></DIV> <DIV><FONT size=3>    }    </FONT></DIV> <DIV><FONT size=3>    public int doEndTag()throws JspTagException</FONT></DIV> <DIV><FONT size=3>    {</FONT></DIV> <DIV><FONT size=3>            try</FONT></DIV> <DIV><FONT size=3>            {</FONT></DIV> <DIV><FONT size=3>                JspWriter out = pageContext.getOut();</FONT></DIV> <DIV><FONT size=3>                con=DriverManager.getConnection(url,"yanguser","1234"); </FONT></DIV> <DIV><FONT size=3>String strQuery = "select id,bookName,publisher,bookAuthor,price,time from BookInfo where id >=100";</FONT></DIV> <DIV><FONT size=3>                state = con.createStatement();</FONT></DIV> <DIV><FONT size=3>                rs = state.executeQuery(strQuery);</FONT></DIV> <DIV><FONT size=3>                </FONT></DIV> <DIV><FONT size=3>                int bookID;</FONT></DIV> <DIV><FONT size=3>                String bookName=null;</FONT></DIV> <DIV><FONT size=3>                String bookPublisher=null;</FONT></DIV> <DIV><FONT size=3>                String bookAuthor=null;</FONT></DIV> <DIV><FONT size=3>                int bookPrice;</FONT></DIV> <DIV><FONT size=3>                String publisherTime;</FONT></DIV> <DIV><FONT size=3>                      </FONT></DIV> <DIV><FONT size=3>                out.write("<table width='780' height='21' border='1'>") ;</FONT></DIV> <DIV><FONT size=3>                out.write("<tr>");</FONT></DIV> <DIV><FONT size=3>                out.write("<td width='66' ><div align='center'>书号</div></td>");</FONT></DIV> <DIV><FONT size=3>                out.write("<td width='145' ><div align='center'>书名 </div></td>");</FONT></DIV> <DIV><FONT size=3>                out.write("<td width='103' ><div align='center'>书的作者 </div></td>");</FONT></DIV> <DIV><FONT size=3>                out.write("<td width='163' ><div align='center'>出版社</div></td>");</FONT></DIV> <DIV><FONT size=3>                out.write("<td width='107' ><div align='center'>书的价格</div></td>");</FONT></DIV> <DIV><FONT size=3>                out.write("<td width='156' ><div align='center'>出版日期</div></td>");</FONT></DIV> <DIV><FONT size=3>                out.write("</tr>");</FONT></DIV> <DIV><FONT size=3>                while(rs.next())</FONT></DIV> <DIV><FONT size=3>                {</FONT></DIV> <DIV><FONT size=3>                    bookID = rs.getInt("id");</FONT></DIV> <DIV><FONT size=3>                    bookName = rs.getString("bookName");</FONT></DIV> <DIV><FONT size=3>                    bookPublisher = rs.getString("publisher");</FONT></DIV> <DIV><FONT size=3>                    bookAuthor = rs.getString("bookAuthor");</FONT></DIV> <DIV><FONT size=3>                    bookPrice = rs.getInt("price");</FONT></DIV> <DIV><FONT size=3>                    publisherTime=rs.getString("time");</FONT></DIV> <DIV><FONT size=3>                    </FONT></DIV> <DIV><FONT size=3>                    out.write("<tr>");    </FONT></DIV> <DIV><FONT size=3>                        out.write("<td width='66'>"+bookID+"</td>");</FONT></DIV> <DIV><FONT size=3>                        out.write("<td width='145'>"+bookName+"</td>");</FONT></DIV> <DIV><FONT size=3>                        out.write("<td width='103' >"+bookAuthor+"</td>");                                     out.write("<td width='163'>"+bookPublisher+"</td>");</FONT></DIV> <DIV><FONT size=3>                        out.write("<td width='107'>"+bookPrice+"</td>"); </FONT></DIV> <DIV><FONT size=3>                        out.write("<td width='156'>"+publisherTime+"</td>");                                out.write("</tr>"); </FONT></DIV> <DIV><FONT size=3>              }</FONT></DIV> <DIV><FONT size=3>              out.write("</table>");       </FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>        catch(Exception e)</FONT></DIV> <DIV><FONT size=3>        {</FONT></DIV> <DIV><FONT size=3>           throw new JspTagException("不能正确地连接数据库!"); </FONT></DIV> <DIV><FONT size=3>        }</FONT></DIV> <DIV><FONT size=3>      return EVAL_PAGE;</FONT></DIV> <DIV><FONT size=3>    }</FONT></DIV> <DIV><FONT size=3>}</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(2)</B><B>编写TLD文件,请见GetDBDataTag.tld文件</B></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3><?xml version="1.0" encoding="ISO-8859-1" ?></FONT></DIV> <DIV><FONT size=3><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"</FONT></DIV> <DIV><FONT size=3>    ""></FONT></DIV> <DIV><FONT size=3>    <taglib></FONT></DIV> <DIV><FONT size=3>      <tlibversion>1.0</tlibversion></FONT></DIV> <DIV><FONT size=3>      <jspversion>1.1</jspversion>                      </FONT></DIV> <DIV><FONT size=3>      <shortname>getDBTag</shortname>                       </FONT></DIV> <DIV><FONT size=3>      <uri></uri> </FONT></DIV> <DIV><FONT size=3>      <info>标签库的使用描述信息</info></FONT></DIV> <DIV><FONT size=3>      <tag>                                 </FONT></DIV> <DIV><FONT size=3>        <name> GetDBDataTag </name>                     </FONT></DIV> <DIV><FONT size=3>        <tagclass>jspTagLib.GetDBDataTag </tagclass>    </FONT></DIV> <DIV><FONT size=3>        <info>标签的描述信息</info></FONT></DIV> <DIV><FONT size=3>        <bodycontent>JSP</bodycontent></FONT></DIV> <DIV><FONT size=3>      </tag> </FONT></DIV> <DIV><FONT size=3>    </taglib></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(3)在部署web.xml文件中添加如下的信息</FONT></B></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3><taglib>  </FONT></DIV> <DIV><FONT size=3>    <taglib-uri>/taglib/GetDBDataTag.tld</taglib-uri>           </FONT></DIV> <DIV><FONT size=3>    <taglib-location></FONT></DIV> <DIV><FONT size=3>          /WEB-INF/taglib/GetDBDataTag.tld                  </FONT></DIV> <DIV><FONT size=3>    </taglib-location></FONT></DIV> <DIV><FONT size=3>  </taglib></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(4)在JSP页面中使用该标签</FONT></B></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3><%@ page contentType="text/html;charset=gb2312" errorPage="DealError.jsp"%></FONT></DIV> <DIV><FONT size=3><%@ taglib uri="/taglib/GetDBDataTag.tld" prefix="getDBTag" %></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3>….</FONT></DIV> <DIV><FONT size=3><getDBTag:GetDBDataTag/></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>六、访问数据库的带属性的</B><B>标签设计例</B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(1)</B><B>标签处理程序,请见</B><B>GetDBDataWithAttrTag.java</B><B>文件。</B></FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>在该程序中实现数据库的连接,并从标签所带的属性中获得属性的值。该值将作为数据库表的操作条件,然后将操作的结果直接输出到页面上,实现某一独立的功能。</FONT></B></DIV> <DIV><FONT face="Times New Roman" size=3>package jspTagLib;</FONT></DIV> <DIV><FONT face="Times New Roman" size=3>import javax.servlet.jsp.*;</FONT></DIV> <DIV><FONT face="Times New Roman" size=3>import javax.servlet.jsp.tagext.*;</FONT></DIV> <DIV><FONT face="Times New Roman" size=3>import java.sql.*;</FONT></DIV> <DIV><FONT face="Times New Roman" size=3>public class GetDBDataWithAttrTag extends TagSupport</FONT></DIV> <DIV><FONT face="Times New Roman" size=3>{ </FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   Connection con = null;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   PreparedStatement pstate = null;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   ResultSet rs = null;</FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3>   </FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   String url="jdbc:odbc:WebMisDB";</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";          </FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3>   </FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   private int minID; </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   private int maxID; </FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3>/*</FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">       </FONT>下面是属性的<FONT face="Times New Roman">get</FONT>和<FONT face="Times New Roman">set</FONT>方法<FONT face="Times New Roman">.</FONT>注意其方法的命名规则</FONT></DIV> <DIV><FONT face="Times New Roman" size=3>*/</FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman"> public void setMinID(String newMinID)</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">      {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           this.minID = Integer.parseInt(newMinID);</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    } </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    public String getMinID()</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           return new Integer(this.minID).toString();</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    }</FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3>    </FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">      public void setMaxID(String newMaxID)</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">      {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           this.maxID = Integer.parseInt(newMaxID);</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    } </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    public String getMaxID()</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           return new Integer(this.maxID).toString();</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    }    </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   public GetDBDataWithAttrTag()throws ClassNotFoundException</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">      Class.forName(DBDriver);</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">   }</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    public int doStartTag() throws JspTagException </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">              return EVAL_BODY_INCLUDE;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    }</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">      public int doEndTag()throws JspTagException</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">      {</FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3>/*</FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">       out</FONT>是一个<FONT face="Times New Roman">JspWriter</FONT>类型的对象,用于向<FONT face="Times New Roman">jsp</FONT>网页输出内容。输出的目标由<FONT face="Times New Roman">pageContext.getOut</FONT>提供<FONT face="Times New Roman"> ,</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">       </FONT>其中<FONT face="Times New Roman">pageContext</FONT>是<FONT face="Times New Roman">TagSupport</FONT>类中的一个<FONT face="Times New Roman">PageContext</FONT>类的对象</FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">*/           </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                  try</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                  {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        JspWriter out = pageContext.getOut();</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        con=DriverManager.getConnection(url,"yanguser","1234"); </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        String strQuery = "select id,bookName,publisher,bookAuthor,price,time from BookInfo where id >= ? and id<= ?";</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        pstate = con.prepareStatement(strQuery,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);</FONT></FONT></DIV> <DIV><B><FONT size=3><FONT face="Times New Roman">                        pstate.setInt(1,minID);</FONT></FONT></B></DIV> <DIV><B><FONT size=3><FONT face="Times New Roman">                        pstate.setInt(2,maxID);</FONT></FONT></B></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        rs = pstate.executeQuery();</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                      int bookID;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                         String bookName=null;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                         String bookPublisher=null;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                         String bookAuthor=null;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                         int bookPrice;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                         String publisherTime;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                               </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<table width='780' height='21' border='1'>") ;</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<tr>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<td width='66' ><div align='center'></FONT>书号<FONT face="Times New Roman"></div></td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<td width='145' ><div align='center'></FONT>书名 <FONT face="Times New Roman"></div></td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<td width='103' ><div align='center'></FONT>书的作者 <FONT face="Times New Roman"></div></td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<td width='163' ><div align='center'></FONT>出版社<FONT face="Times New Roman"></div></td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<td width='107' ><div align='center'></FONT>书的价格<FONT face="Times New Roman"></div></td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("<td width='156' ><div align='center'></FONT>出版日期<FONT face="Times New Roman"></div></td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                            out.write("</tr>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        while(rs.next())</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                  bookID = rs.getInt("id");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                bookName = rs.getString("bookName");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                  bookPublisher = rs.getString("publisher");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                bookAuthor = rs.getString("bookAuthor");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                  bookPrice = rs.getInt("price");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                  publisherTime=rs.getString("time");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                   out.write("<tr>");    </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                         out.write("<td width='66'>"+bookID+"</td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                         out.write("<td width='145'>"+bookName+"</td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                         out.write("<td width='103' >"+bookAuthor+"</td>");                                   </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                         out.write("<td width='163'>"+bookPublisher+"</td>");</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                         out.write("<td width='107'>"+bookPrice+"</td>"); </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                                          out.write("<td width='156'>"+publisherTime+"</td>");                                                                    out.write("</tr>"); </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                    }</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                       out.write("</table>");       </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           }</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           catch(Exception e)</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           {</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                 throw new JspTagException("</FONT>不能正确地连接数据库<FONT face="Times New Roman">!"); </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           }</FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">        return EVAL_PAGE;</FONT></FONT></DIV> <DIV><FONT face="Times New Roman"><FONT size=3>      }</FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3>}</FONT></DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><FONT size=3><B>(2)TLD文件,请见<FONT face="Times New Roman">GetDBDataWithAttrTag</FONT></B><B>.tld</B><B>文件</B></FONT></DIV> <DIV><FONT face="Times New Roman" size=3></FONT> </DIV> <DIV><FONT face="Times New Roman" size=3><?xml version="1.0" encoding="ISO-8859-1" ?></FONT></DIV> <DIV><FONT face="Times New Roman" size=3><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"</FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">       ""></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">       <taglib></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">         <tlibversion>1.0</tlibversion></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">         <jspversion>1.1</jspversion>                                       </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">         <shortname>getDBWithAttrTag</shortname>                                     </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">         <uri></uri> </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">         <info></FONT>标签库的使用描述信息<FONT face="Times New Roman"></info>                                               </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">         <tag>                                                          </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           <name> GetDBDataWithAttrTag </name>                                       </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           <tagclass>jspTagLib.GetDBDataWithAttrTag </tagclass>    </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           <info></FONT>标签的描述信息<FONT face="Times New Roman"></info></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">           <bodycontent>JSP</bodycontent></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                <attribute></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                    <name>minID</name>   <!--</FONT>参数名<FONT face="Times New Roman">--></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                           <required>true</required></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                        <rtexprvalue>true</rtexprvalue>   <!--</FONT>是否允许表达式输出<FONT face="Times New Roman">--></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                </attribute></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                <attribute></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                           <name>maxID</name></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                           <required>true</required></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                           <rtexprvalue>true</rtexprvalue></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">                </attribute></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">      </tag> </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">       </taglib></FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3></FONT> </DIV> <DIV><B><FONT size=3>(3)TOMCAT部署web.xml文件</FONT></B></DIV> <DIV><FONT face="Times New Roman" size=3></FONT> </DIV> <DIV><FONT face="Times New Roman" size=3><taglib></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    <taglib-uri>/taglib/GetDBDataWithAttrTag.tld</taglib-uri>                    </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    <taglib-location></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">          /WEB-INF/taglib/GetDBDataWithAttrTag.tld                                </FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman">    </taglib-location></FONT></FONT></DIV> <DIV><FONT size=3><FONT face="Times New Roman"> </taglib></FONT></FONT></DIV> <DIV><FONT face="Times New Roman" size=3></FONT> </DIV> <DIV><B><FONT size=3></FONT></B> </DIV> <DIV><B><FONT size=3>(4)在JSP页面中使用该标签</FONT></B></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3><%@ page contentType="text/html;charset=gb2312" errorPage="DealError.jsp"%></FONT></DIV> <DIV><FONT size=3><%@ taglib uri="/taglib/GetDBDataWithAttrTag.tld" prefix="getDBWithAttrTag" %></FONT></DIV> <DIV><FONT size=3></FONT> </DIV> <DIV><FONT size=3>….</FONT></DIV> <DIV><FONT size=3>  <getDBWithAttrTag:GetDBDataWithAttrTag minID='100' maxID='1000' /></FONT></DIV> <DIV><FONT face="Times New Roman" size=3></FONT> </DIV></DIV> </div> <!-- <div class="Blog_con3_1">管理员在2009年8月13日编辑了该文章文章。</div> --> <div class="Blog_con2_1 Blog_con3_2"> <div> <!--<img src="/image/default/tu_8.png">--> <!-- JiaThis Button BEGIN --> <div class="bdsharebuttonbox"><A class=bds_more href="#" data-cmd="more"></A><A class=bds_qzone title=分享到QQ空间 href="#" data-cmd="qzone"></A><A class=bds_tsina title=分享到新浪微博 href="#" data-cmd="tsina"></A><A class=bds_tqq title=分享到腾讯微博 href="#" data-cmd="tqq"></A><A class=bds_renren title=分享到人人网 href="#" data-cmd="renren"></A><A class=bds_weixin title=分享到微信 href="#" data-cmd="weixin"></A></div> <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> <!-- JiaThis Button END --> </div> 阅读(480) | 评论(0) | 转发(0) | <div class="HT_line3"></div> </div> <div class="Blog_con3_3"> <div><span id='digg_num'>0</span><a href="javascript:void(0)" id='digg' bid='400867' url='/blog/digg.html' ></a></div> <p>上一篇:<a href="/uid-22171242-id-400866.html">java培训日志☆session</a></p> <p>下一篇:<a href="/uid-22171242-id-400869.html">JavaScript培训日志☆2</a></p> </div> </div> <!-- <div class="Blog_con3_4 Blog_con3_5"> <div class="Blog_tit2 Blog_tit7">热门推荐</div> <ul> <li><a href="" title="" target='blank' ></a></li> </ul> </div> --> </div> </div> <div class="Blog_right1_7" id='replyList'> <div class="Blog_tit3">给主人留下些什么吧!~~</div> <!--暂无内容--> <!-- 评论分页--> <div class="Blog_right1_6 Blog_right1_12"> </div> <!-- 评论分页--> <div class="Blog_right1_10" style="display:none"> <div class="Blog_tit3">评论热议</div> <!--未登录 --> <div class="Blog_right1_8"> <div class="nologin_con1"> 请登录后评论。 <p><a href="http://account.chinaunix.net/login" onclick="link(this)">登录</a> <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net">注册</a></p> </div> </div> </div> <div style="text-align:center;margin-top:10px;"> <script type="text/javascript" smua="d=p&s=b&u=u3118759&w=960&h=90" src="//www.nkscdn.com/smu0/o.js"></script> </div> </div> </div> </div> <input type='hidden' id='report_url' value='/blog/ViewReport.html' /> <script type="text/javascript"> //测试字符串的长度 一个汉字算2个字节 function mb_strlen(str) { var len=str.length; var totalCount=0; for(var i=0;i<len;i++) { var c = str.charCodeAt(i); if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { totalCount++; }else{ totalCount+=2; } } return totalCount; } /* var Util = {}; Util.calWbText = function (text, max){ if(max === undefined) max = 500; var cLen=0; var matcher = text.match(/[^\x00-\xff]/g), wlen = (matcher && matcher.length) || 0; //匹配url链接正则 http://*** var pattern = /http:\/\/([\w-]+\.)+[\w-]+(\/*[\w-\.\/\?%&=][^\s^\u4e00-\u9fa5]*)?/gi; //匹配的数据存入数组 var arrPt = new Array(); var i = 0; while((result = pattern.exec(text)) != null){ arrPt[i] = result[0]; i++; } //替换掉原文本中的链接 for(var j = 0;j<arrPt.length;j++){ text = text.replace(arrPt[j],""); } //减掉链接所占的长度 return Math.floor((max*2 - text.length - wlen)/2 - 12*i); }; */ var allowComment = '0'; //举报弹出层 function showJuBao(url, cid){ $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 , 'blogurl' : window.location.href }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); } $(function(){ //创建管理员删除的弹出层 $('#admin_article_del').click(function(){ $.cover(false); asyncbox.open({ id : 'class_thickbox', html : '<div class="HT_layer3_1"><ul><li class="HT_li1">操作原因:<select class="HT_sel7" id="del_type" name="del_type"><option value="广告文章">广告文章</option><option value="违规内容">违规内容</option><option value="标题不明">标题不明</option><option value="文不对题">文不对题</option></select></li><li class="HT_li1" ><input class="HT_btn4" id="admin_delete" type="button" /></li></ul></div>', title : '选择类型', width : 300, height : 150, scroll : 'no', callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); $('#admin_delete').live('click' , function(){ ///blog/logicdel/id/3480184/url/%252Fblog%252Findex.html.html var type = $('#del_type').val(); var url = '/blog/logicdel/id/400867/url/%252Fuid%252F22171242.html.html'; window.location.href= url + '?type=' + type; }); //顶 js中暂未添加&过滤 $('#digg').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $('#digg').attr('bid'); var url = $('#digg').attr('url'); var digg_str = $.cookie('digg_id'); if(digg_str != null) { var arr= new Array(); //定义一数组 arr = digg_str.split(","); //字符分割 for( i=0 ; i < arr.length ; i++ ) { if(bid == arr[i]) { showErrorMsg('已经赞过该文章', '消息提示'); return false; } } } $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0) { var num = parseInt($('#digg_num').html(),10); num += 1; $('#digg_num').html(num); $('#digg').die(); if(digg_str == null) { $.cookie('digg_id', bid, {expires: 30 , path: '/'}); } else { $.cookie('digg_id', digg_str + ',' + bid, {expires: 30 , path: '/'}); } showSucceedMsg('谢谢' , '消息提示'); } else if(msg.error == 1) { //showErrorMsg(msg.error_content , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } else if(msg.error == 2) { showErrorMsg(msg.error_content , '消息提示'); } } }); }); //举报弹出层 /*$('.box_report').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var url = $('#report_url').val(); var cid = $(this).attr('cid'); $.cover(false); asyncbox.open({ id : 'report_thickbox', url : url, title : '举报违规', width : 378, height : 240, scroll : 'no', data : { 'cid' : cid, 'idtype' : 2 }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); });*/ //评论相关代码 //点击回复显示评论框 $('.Blog_a10').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var tid = $(this).attr('toid');//留言作者id var bid = $(this).attr('bid');//blogid var cid = $(this).attr('cid');//留言id var tname = $(this).attr('tname'); var tpl = '<div class="Blog_right1_9">'; tpl += '<div class="div2">'; tpl += '<textarea name="" cols="" rows="" class="Blog_ar1_1" id="rmsg">文明上网,理性发言...</textarea>'; tpl += '</div>'; tpl += '<div class="div3">'; tpl += '<div class="div3_2"><a href="javascript:void(0);" class="Blog_a11" id="quota_sbumit" url="/Comment/PostComment.html" tid="'+tid+'" bid="'+bid+'" cid="'+cid+'" tname="'+tname+'" ></a><a href="javascript:void(0)" id="qx_comment" class="Blog_a12"></a></div>'; tpl += '<div class="div3_1"><a href="javascript:void(0);" id="mface"><span></span>表情</a></div>'; tpl += '<div class="clear"></div>'; tpl += '</div>'; tpl += '</div>'; $('.z_move_comment').html(''); $(this).parents('.Blog_right1_8').find('.z_move_comment').html(tpl).show(); }); //引用的评论提交 $('#quota_sbumit').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var tid = $(this).attr('tid');//被引用人的id var qid = $(this).attr('cid');//引用的id var url = $(this).attr('url'); var text = $('#rmsg').val(); var tname = $(this).attr('tname'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : tid , 'qid' : qid , 'text': text , 'tname' : tname }, dataType: 'json', success: function(data){ if(data.code == 1){ var tpl = '<div class="Blog_right1_8">'; tpl+= '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl+= '<div class="Blog_right_font1">'; tpl+= '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl+= '<p class="Blog_p7"><a href="' + data.info.quota.url + '">' + data.info.quota.username + '</a>:'+ data.info.quota.content + '</p>'; tpl+= '<p class="Blog_p8">' + data.info.content + '</p><span class="span_text1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + ' tname = ' + data.info.username + ' >回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '" >删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '" >举报</a></span></div>'; tpl+= '<div class="z_move_comment" style="display:none"></div>'; tpl+= '<div class="Blog_line1"></div></div>'; $('#replyList .Blog_right1_8:first').before(tpl); $('.z_move_comment').html('').hide(); } else if(data.code == -1){ //showErrorMsg(data.info , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); } }, error: function(){//请求出错处理 } }); }); //底部发表评论 $('#submitmsg').click(function(){ if(allowComment == 1) { showErrorMsg('该博文不允许评论' , '消息提示'); return false; } var bid = $(this).attr('bid'); var toid = $(this).attr('toid'); var text = $('#reply').val(); var url = $(this).attr('url'); if(text == '' || text=='文明上网,理性发言...') { showErrorMsg('评论内容不能为空!' , '消息提示'); return false; } else { if(mb_strlen(text) > 1000){ showErrorMsg('评论内容不能超过500个汉字' , '消息提示'); return false; } } $.ajax({ type: "post", url: url , data: {'bid': bid , 'to' : toid ,'text': text}, dataType: 'json', success: function(data){ if(data.code == 1) { var tpl = '<div class="Blog_right1_8">'; tpl += '<div class="Blog_right_img1"><a href="' +data.info.url+ '" >' + data.info.header + '</a></div>'; tpl += '<div class="Blog_right_font1">'; tpl += '<p class="Blog_p5"><span><a href="' +data.info.url+ '" >' + data.info.username + '</a></span>' + data.info.dateline + '</p>'; tpl += '<p class="Blog_p6">' + data.info.content + '</p>'; tpl += '<div class="div1"><a href="javascript:void(0);" class="Blog_a10" toid=' + data.info.fuid + ' bid=' + data.info.bid + ' cid=' + data.info.cid + '>回复</a> |  <a class="comment_del_mark" style="cursor:pointer" url="' + data.info.delurl + '">删除</a> |  <a href="javascript:showJuBao(\'/blog/ViewReport.html\','+data.info.cid+')" class="box_report" cid="' + data.info.cid + '">举报</a></div>'; tpl += '<div class="z_move_comment" style="display:none"></div>'; tpl += '</div><div class="Blog_line1"></div></div>'; $('.Blog_tit3:first').after(tpl); $('#reply').val('文明上网,理性发言...'); } else if(data.code == -1) { showErrorMsg(data.info , '消息提示'); } }, error: function(){//请求出错处理 } }); }); //底部评论重置 $('#reset_comment').click(function(){ $('#reply').val('文明上网,理性发言...'); }); //取消回复 $('#qx_comment').live('click' , function(){ $('.z_move_comment').html('').hide(); }); $('#rmsg, #reply').live({ focus:function(){ if($(this).val() == '文明上网,理性发言...'){ $(this).val(''); } }, blur:function(){ if($(this).val() == ''){ $(this).val('文明上网,理性发言...'); } } }); //删除留言确认 $('.comment_del_mark').live('click' , function(){ var url = $(this).attr('url'); asyncbox.confirm('删除留言','确认', function(action){ if(action == 'ok') { location.href = url; } }); }); //删除时间确认 $('.del_article_id').click(function(){ var delurl = $(this).attr('delurl'); asyncbox.confirm('删除文章','确认', function(action){ if(action == 'ok') { location.href = delurl; } }); }); /* //字数限制 $('#rmsg, #reply').live('keyup', function(){ var id = $(this).attr('id'); var left = Util.calWbText($(this).val(), 500); var eid = '#errmsg'; if(id == 'reply') eid = '#rerrmsg'; if (left >= 0) $(eid).html('您还可以输入<span>' + left + '</span>字'); else $(eid).html('<font color="red">您已超出<span>' + Math.abs(left) + '</span>字 </font>'); }); */ //加载表情 $('#face').qqFace({id : 'facebox1', assign : 'reply', path : '/image/qqface/'}); $('#mface').qqFace({id : 'facebox', assign : 'rmsg', path:'/image/qqface/'}); /* $('#class_one_id').change(function(){ alert(123213); var id = parseInt($(this).val() , 10); if(id == 0) return false; $('.hidden_son_class span').each(function( index , dom ){ if( dom.attr('cid') == id ) { } }); }); */ //转载文章 var turn_url = "/blog/viewClassPart.html"; $('#repost_bar').click(function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var id = $(this).attr('bid'); asyncbox.open({ id : 'turn_class_thickbox', url : turn_url, title : '转载文章', width : 330, height : 131, scroll : 'no', data : { 'id' : id }, callback : function(action){ if(action == 'close'){ $.cover(false); } } }); }); /* //转发文章 $('#repost_bar').live('click' , function(){ if(isOnLine == '' ) { //showErrorMsg('登录之后才能进行此操作' , '消息提示'); showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); return false; } var bid = $(this).attr('bid'); var url = $(this).attr('url'); asyncbox.confirm('转载文章','确认', function(action){ if(action == 'ok'){ $.ajax({ type:"POST", url:url, data: { 'bid' : bid }, dataType: 'json', success:function(msg){ if(msg.error == 0){ showSucceedMsg('转发成功!', '消息提示'); }else if(msg.error == 1){ //location.href = '/index.php?r=site/login'; showErrorMsg('操作失败,您需要先登录!', '消息提示', 'http://account.chinaunix.net/login'); }else{ showErrorMsg(msg.error_content, '消息提示'); } } }); } }); }); */ }); </script> <!--该部分应该放在输出代码块的后面才起作用 --> <script type="text/javascript"> SyntaxHighlighter.autoloader( 'applescript /highlight/scripts/shBrushAppleScript.js', 'actionscript3 as3 /highlight/scripts/shBrushAS3.js', 'bash shell /highlight/scripts/shBrushBash.js', 'coldfusion cf /highlight/scripts/shBrushColdFusion.js', 'cpp c /highlight/scripts/shBrushCpp.js', 'c# c-sharp csharp /highlight/scripts/shBrushCSharp.js', 'css /highlight/scripts/shBrushCss.js', 'delphi pascal /highlight/scripts/shBrushDelphi.js', 'diff patch pas /highlight/scripts/shBrushDiff.js', 'erl erlang /highlight/scripts/shBrushErlang.js', 'groovy /highlight/scripts/shBrushGroovy.js', 'java /highlight/scripts/shBrushJava.js', 'jfx javafx /highlight/scripts/shBrushJavaFX.js', 'js jscript javascript /highlight/scripts/shBrushJScript.js', 'perl pl /highlight/scripts/shBrushPerl.js', 'php /highlight/scripts/shBrushPhp.js', 'text plain /highlight/scripts/shBrushPlain.js', 'py python /highlight/scripts/shBrushPython.js', 'ruby rails ror rb /highlight/scripts/shBrushRuby.js', 'scala /highlight/scripts/shBrushScala.js', 'sql /highlight/scripts/shBrushSql.js', 'vb vbnet /highlight/scripts/shBrushVb.js', 'xml xhtml xslt html /highlight/scripts/shBrushXml.js' ); SyntaxHighlighter.all(); function code_hide(id){ var code = document.getElementById(id).style.display; if(code == 'none'){ document.getElementById(id).style.display = 'block'; }else{ document.getElementById(id).style.display = 'none'; } } </script> <!--回顶部js2011.12.30--> <script language="javascript"> lastScrollY=0; function heartBeat(){ var diffY; if (document.documentElement && document.documentElement.scrollTop) diffY = document.documentElement.scrollTop; else if (document.body) diffY = document.body.scrollTop else {/*Netscape stuff*/} percent=.1*(diffY-lastScrollY); if(percent>0)percent=Math.ceil(percent); else percent=Math.floor(percent); document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px"; lastScrollY=lastScrollY+percent; if(lastScrollY<200) { document.getElementById("full").style.display="none"; } else { document.getElementById("full").style.display="block"; } } var gkuan=document.body.clientWidth; var ks=(gkuan-960)/2-30; suspendcode="<div id=\"full\" style='right:-30px;POSITION:absolute;TOP:500px;z-index:100;width:26px; height:86px;cursor:pointer;'><a href=\"javascript:void(0)\" onclick=\"window.scrollTo(0,0);\"><img src=\"\/image\/top.png\" /></a></div>" document.write(suspendcode); window.setInterval("heartBeat()",1); </script> <!-- footer --> <div class="Blog_footer" style='clear:both'> <div><a href="http://www.chinaunix.net/about/index.shtml" target="_blank" rel="nofollow">关于我们</a> | <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank" rel="nofollow">关于IT168</a> | <a href="http://www.chinaunix.net/about/connect.html" target="_blank" rel="nofollow">联系方式</a> | <a href="http://www.chinaunix.net/about/service.html" target="_blank" rel="nofollow">广告合作</a> | <a href="http://www.it168.com//bottomfile/flgw/fl.htm" target="_blank" rel="nofollow">法律声明</a> | <a href="http://account.chinaunix.net/register?url=http%3a%2f%2fblog.chinaunix.net" target="_blank" rel="nofollow">免费注册</a> <p>Copyright 2001-2010 ChinaUnix.net All Rights Reserved 北京皓辰网域网络信息技术有限公司. 版权所有 </p> <div>感谢所有关心和支持过ChinaUnix的朋友们 <p><a href="http://beian.miit.gov.cn/">16024965号-6 </a></p> </div> </div> </div> </div> <script language="javascript"> //全局错误提示弹出框 function showErrorMsg(content, title, url){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span1"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="login_btn1" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //全局正确提示 function showSucceedMsg(content, title , url ){ var url = url || ''; var title = title || '消息提示'; var html = ''; html += '<div class="HT_layer3_1 HT_layer3_2"><ul><li><p><span class="login_span2"></span>' + content + '</p></li>'; if(url == '' || url.length == 0){ html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick=\'close_windows("error_msg")\'></li>'; } else { html += '<li class="HT_li1"><input type="button" class="HT_btn2" onclick="location.href=\'' + url + '\'"></li>'; } html += '</ul></div>'; $.cover(true); asyncbox.open({ id: 'error_msg', title : title, html : html, 'callback' : function(action){ if(action == 'close'){ $.cover(false); } } }); } //关闭指定id的窗口 function close_windows(id){ $.cover(false); $.close(id); } //公告 var tID; var tn; // 高度 var nStopTime = 5000; // 不同行间滚动时间隔的时间,值越小,移动越快 var nSpeed = 50; // 滚动时,向上移动一像素间隔的时间,值越小,移动越快 var isMove = true; var nHeight = 25; var nS = 0; var nNewsCount = 3; /** * n 用于表示是否为第一次运行 **/ function moveT(n) { clearTimeout(tID) var noticev2 = document.getElementById("noticev2") nS = nSpeed; // 只在第一次调用时运行,初始化环境(有没有参数) if (n) { // 设置行高 noticev2.style.lineHeight = nHeight + "px"; // 初始化显示位置 tn = 0; // 刚进入时在第一行停止时间 nS = nStopTime; } // 判断鼠标是否指向层 if (isMove) { // 向上移动一像素 tn--; // 如果移动到最下面一行了,则移到顶行 if (Math.abs(tn) == nNewsCount * nHeight) { tn = 0; } // 设置位置 noticev2.style.marginTop = tn + "px"; // 完整显示一行时,停止一段时间 if (tn % nHeight == 0) { nS = nStopTime; } } tID = setTimeout("moveT()", nS); } moveT(1); // 此处可以传入任何参数 </script> <script type="text/javascript"> // var _gaq = _gaq || []; // _gaq.push(['_setAccount', 'UA-20237423-2']); // _gaq.push(['_setDomainName', '.chinaunix.net']); // _gaq.push(['_trackPageview']); // // (function() { // var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; // ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; // var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); // })(); </script> <script type="text/javascript"> var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F0ee5e8cdc4d43389b3d1bfd76e83216b' type='text/javascript'%3E%3C/script%3E")); function link(t){ var href= $(t).attr('href'); href+="?url="+encodeURIComponent(location.href); $(t).attr('href',href); //setCookie("returnOutUrl", location.href, 60, "/"); } </script> </body> </html>