icefeeling的家园

生活就是一本书,需要我们用心去读

 

JSP自定义标签试验

一、概述

       JSP中有一块重要的技术:自定义标签(Custom Tag),最近这几天在学习Struts的时候发现Struts中使用了很多自定义标签,如html、bean等。所以我就做了个简单的试验,学习一下这种技术。

       首先介绍一下这种技术吧!

1.优点:

取代了JSP中的Java程序,并且可以重复使用,方便不熟悉Java编程的网页设计人员。

2.开发流程:

(1)       编写JSP,在JSP中使用自定义标签。

(2)       在web.xml中指定JSP中使用的标签的.tld(标签库描述文件)文件的位置。

(3)       .tld文件中指定标签使用的类。

3. 自定义标签的分类:

(1)       简单标签:如< mytaghelloworld/>

(2)       带属性标签:如<imytagcheckinput dbname = “<myBean.getDBName()>”/>

(3)       带标签体的标签:

在自定义标签的起始和结束标签之间的部分为标签体(Body)Body的内容可以是JSP中的标准标签,也可以是HTML、脚本语言或其他的自定义标签。

<mytagcheckinput dbname = “<myBean.getDBName()>”>

      <mytag:log message=”Table Name”>

<mytagcheckinput />

(4)       可以被scrīpt使用的标签:

定义了idtype属性的标签可以被标签后面的scrīptlet使用。

<mytagconnection id = “oraDB” type = “DataSource” name = “Oracle”>

<%oraDB.getConnection(); %>

 

4.接口及其他

实际上,自定义标签的处理类实现了Tag Handler对象。JSP技术在javax.servlet.jsp.tagext中提供了多个Tag Handler接口,JSP1.2中定义了Tag、BodyTag、IterationTag接口,在JSP2.0中新增了SimpleTag接口。JSP还提供了上述接口的实现类TagSupport、BodyTagSupport和SimpleTagSupport(SimpleTagSupport只在JSP2.0中才有)。BodyTagSupport实现了BodyTag、Tag和IterationTag接口。

 

接口及其方法

Tag接口

方法

SimpleTag

dotage

Tag

doStartTag,doEndTag,release

IterationTag

doStartTag,doAfterTag,release

BodyTag

doStartTag,doEndTag,release,doInitBody,doAfterBody

 

下表引自Sun的JSP在线教程。

Tag Handler Methods

Tag Handler Type

Methods

Simple

doStartTag, doEndTag, release

Attributes

doStartTag, doEndTag, set/getAttribute1...N, release

Body, Evaluation and No Interaction

doStartTag, doEndTag, release

Body, Iterative Evaluation

doStartTag, doAfterBody, doEndTag, release

Body, Interaction

doStartTag, doEndTag, release, doInitBody, doAfterBody, release

 

下表中的EVAL是evaluate的缩写,意思是:评价, 估计, 求...的值,在下列的返回值中的意思是执行。

返回值

意义

SKIP_BODY

表示不用处理标签体,直接调用doEndTag()方法。

SKIP_PAGE

忽略标签后面的JSP内容。

EVAL_PAGE

处理标签后,继续处理JSP后面的内容。

EVAL_BODY_BUFFERED

表示需要处理标签体。

EVAL_BODY_INCLUDE

表示需要处理标签体,但绕过setBodyContent()和doInitBody()方法

EVAL_BODY_AGAIN

对标签体循环处理。

 

具体用法可以查看其他参考资料。

Sun的Java教程相关部分:http://java.sun.com/webservices/docs/1.0/tutorial/doc/JSPTags.html

二、实验

  

自定义标签三步曲:

    1 定义标签类

属性等

路径:

类内容:必须继承TagSupport这个类。属性的getset方法也必须写。

package appcode.helloword;

 

import javax.servlet.jsp.tagext.TagSupport;

import javax.servlet.jsp.*;

import java.io.*;

 

public class TagTest extends TagSupport

{

    // 标签属性begin

    private String begin = null;

    // 标签属性end

    private String end = null;

    // 构造函数

    public TagTest(){

     

    }

 

    /* 标签初始方法 */

    public int doStartTag() throws JspTagException{

          return super.EVAL_BODY_INCLUDE;

    }

 

    /* 标签结束方法 */

    public int doEndTag() throws JspTagException{

          JspWriter out = pageContext.getOut();

          String sum = begin + end;

          try{

           //标签的返回值

           out.println(sum);

          }catch(IOException e){

           e.printStackTrace();

          }

          return super.SKIP_BODY;

    }

 

    /* 释放资源 */

    public void release(){

          super.release();

    }

 

    public String getBegin() {

        return begin;

    }

 

    public void setBegin(String begin) {

        this.begin = begin;

    }

 

    public String getEnd() {

        return end;

    }

 

    public void setEnd(String end) {

        this.end = end;

    }

}

2 定义.tld文件

路径:

 

Tld内容:这里tagclass的路径必须和classes下面的TagTest的录入一致

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>

<tlibversion>1.0</tlibversion>

<jspversion>1.1</jspversion>

<shortname>eRedLab JSPTag Library</shortname>

<uri>/testTag</uri>

<info>自定义标签测试</info>

<tag>

      <name>hello</name>

     <tagclass>appcode.helloword.TagTest</tagclass>

      <bodycontent>empty</bodycontent>

      <info>自定义标签测试</info>

      <attribute>

       <name>begin</name>

       <required>true</required>

      </attribute>

      <attribute>

       <name>end</name>

       <required>true</required>

      </attribute>

</tag>

</taglib>

3 配置web.xml文件

路径:

内容:testTag为在页面引用的url

    <taglib>

            <taglib-uri>testTag</taglib-uri>

            <taglib-location>/WEB-INF/tags/helloWord.tld</taglib-location>

</taglib>

4\jsp页面引用

<%@ page contentType="text/html; charset=gb2312" %>

<%@ include file="http://www.cnblogs.com/../common/common_page.jsp" %>

<%@ taglib uri="testTag" prefix="mytag"%>

<html>

<head>

<title>综合查询</title>

</head>

<link href="<%=path%>/style/<%=s_style%>/default/styles.css" rel="stylesheet">

<body>

    <mytag:hello begin="You are a smaller pig!" end="Word" />

</body>

</html>

posted on 2008-11-09 19:40  lzb  阅读(280)  评论(0编辑  收藏  举报

导航