jsp_tag 学习笔记

   学习地址:http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/ 

最近要开发java的CMS 系统 只好逼着自己学了,很重要的一部分是自定义标签。研究了一下,还不算太难。

总述:包括 Bodyless Tags 和 Tags with a Body。

1. Develop the Tag Handler

Sample 1: Tag.java

public interface Tag {
   public final static int SKIP_BODY = 0;
   public final static int EVAL_BODY_INCLUDE = 1;
   public final static int SKIP_PAGE = 5;
   public final static int EVAL_PAGE = 6;

   void setPageContext(PageContext pageContext);
   void setParent(Tag parent);
   Tag getParent();
   int doStartTag() throws JspException;
   int doEndTag() throws JspException;
   void release();
}

实现接口或者继承一个已有的类TagSupport,

Sample 2: HelloTag.java

package tags;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class HelloTag implements Tag {
   private PageContext pageContext;
   private Tag parent;

   public HelloTag() {
      super();
   }

   public int doStartTag() throws JspException {
      try {
         pageContext.getOut().print(
         "This is my first tag!");
      } catch (IOException ioe) {
         throw new JspException("Error: 
         IOException while writing to client" 
         + ioe.getMessage());
      }
      return SKIP_BODY;
   }

   public int doEndTag() throws JspException {
      return SKIP_PAGE;
   }

   public void release() {
   }

   public void setPageContext(PageContext 
   pageContext) {
      this.pageContext = pageContext;
   }

   public void setParent(Tag parent) {
      this.parent = parent;
   }

   public Tag getParent() {
      return parent;
   }
}

2. Create a Tag Library Descriptor

Sample 3: mytaglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//
        DTD JSP Tag Library 1.1//EN"
        "http://java.sun.com/j2ee/dtds/
        web-jsptaglibrary_1_1.dtd">

<!-- a tag library descriptor -->

<taglib>
   <tlibversion>1.0</tlibversion>
   <jspversion>1.1</jspversion>
   <shortname>first</shortname>
   <uri></uri>
   <info>A simple tab library for the 
   examples</info>

  <tag>
    <name>hello</name>
    <tagclass>tags.HelloTag</tagclass>
    <bodycontent>empty</bodycontent>
    <info>Say Hi</info>
  </tag>         
</taglib>
3. Test the Tag

The final step is to test the tag we have developed. In order to use the tag, we have to reference it, and this can be done in three ways:

  1. Reference the tag library descriptor of an unpacked tag library. For example:

     

    <@ taglib uri="/WEB-INF/jsp/mytaglib.tld" 
    prefix="first" %>
    

     

  2. Reference a JAR file containing a tag library. For example:

     

    <@ taglib uri="/WEB-INF/myJARfile.jar" 
    prefix='first" %>
    

     

  3. Define a reference to the tag library descriptor from the web-application descriptor (web.xml) and define a short name to reference the tag library from the JSP. To do this, open the file: c:/tomcat/webapps/examples/web-inf/web.xml and add the following lines before the end line, which is <web-app>:

        <taglib>
           <taglib-uri>mytags</taglib-uri>
           <taglib-location>/WEB-INF/jsp/
           mytaglib.tld</taglib-location>
        </taglib>
    

Now, write a JSP and use the first syntax. Sample 4 shows an example:

Sample 4: Hello.jsp

<%@ taglib uri="/WEB-INF/jsp/mytaglib.tld"
 prefix="first" %>
<HTML>
<HEAD>
<TITLE>Hello Tag</TITLE>
</HEAD>

<BODY bgcolor="#ffffcc">

<B>My first tag prints</B>:

<first:hello/>

</BODY>
</HTML>
先写这么多了 具体的看教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值