logo 西林@生存 - 骑猪闯天下!
学习JSP标签库的一点体会
2006-12-13 17:24:00 By:xling

JSP 标签库

标签库一般包括两个文件,其一是类包JAR文件,其二是以.tld为扩展名的标签符描述文件。这里着重说一下本人在学这部分的体验,不是心得!

一,

标签处理程序(???)要继承javax.servlet.jsp.tagext.TagSupport类。
标签如果有属性的(attribute)话,可以向bean一样写处理程序,即:setAttributeName()和getAttribute()部分和bean的实现一样,在使用标签时,只需按如下格式调用:
代码段一:

<G:test name="xling" sex="M">
Context in testTag.jsp
</G:test>

name和sex是Attribute.

二,tld文件
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">
<taglib>
 <tlibversion>1.0</tlibversion>
 <jspversion>1.1</jspversion>       
 <shortname>DemoTags</shortname>
 <uri>http://xling.com</uri>
 <info>Test Tags Library</info>

 <tag>
  <name>test</name>
  <tagclass>GTag.TestTag</tagclass>
  <bodycontent>JSP</bodycontent>
  <info>Your first JSP Tag</info>
                <attribute>
                <name>name</name>
                <required>true</required>
                <rtexprvalue>false</rtexprvalue>
                </attribute>
                <attribute>
                <name>sex</name>
                <required>false</required>
                <rtexprvalue>false</rtexprvalue>
                </attribute>
 </tag>
</taglib>

水平有限,只说我有所体会的几个属性:
tagclass是指标签处理程序,bodycontent的三个可取值:tagdepent,jsp,empty,如果是empty的话,代码一中所示的开始和结束标签之间不能有任何东东(即:Context in testTag.jsp这一句话)。如果指定为jsp的话,就可以加了。tagdepent暂时没有接触到,不了解。

3,tld的存放位置可随意,因为在WEB-INF/WEB.XML里要说明,来看看这个文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>WebModule1</display-name>
  <jsp-config>
    <taglib>
      <taglib-uri>testTag</taglib-uri>
      <taglib-location>/WEB-INF/testTags.tld</taglib-location>
    </taglib>
    <taglib>
      <taglib-uri>testTags2</taglib-uri>
      <taglib-location>/testTags2.tld</taglib-location>
    </taglib>
  </jsp-config>

</web-app>

<jsp-config>及它的子节点是JBuilder自动维护的,testTags和testTags2要在JSP文件里的如下部分用到:
文件1,

<%@ taglib prefix="G" uri="testTags2" %>


文件2

<%@ taglib prefix="G" uri="testTags" %>



4,标签处理程序示例:

package GTag;

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

public class TestTag extends TagSupport {
   
    private String name,sex;
   
    public TestTag() {
    }
   
    public void setName(String pName){
        name = pName;
    }
   
    public void setSex(String pSex){
        if(pSex.toUpperCase().equals("M"))
            sex = "先生";
        else if(pSex.toUpperCase().equals("F"))
            sex = "女仕";
        else
            sex = pSex;
    }
   
    public int doStartTag() throws JspException{
        try{
            JspWriter jw = pageContext.getOut();
            jw.print("<hr size='1' /> " + name + " " + sex +": welcome you! <br />");
        }catch(Exception e){
            e.printStackTrace();
        }
        return EVAL_BODY_INCLUDE;   
    }
   
    public int doEndTag() throws JspException{
        try{
            JspWriter jw = pageContext.getOut();
            jw.print("<br />Context in doEndTag <hr size='1' />");
        }catch(Exception e){
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }
   
    public void release(){
       
    }
}

doEndTag()可以return的值有如下两个:
EVAL_PAGE和SKIP_PAGE,如果是SKIP_PAGE的话,在这个标签后的不管是HTML还是JSP代码都将截断,不信可以在运行JSP文件后,查看网页源码。

5,调用标签的JSP文件:

<%@page contentType="text/html; charset=GBK"%>
<%@taglib prefix="G" uri="testTag" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.Date" %>
<html>
<head>
<title>testTag</title>
</head>
<body bgcolor="#ffffff">
<G:test name="xling" sex="M">
Context in testTag.jsp
</G:test>

<%
Date d = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
out.println(sf.format(d));
%>
</body>
</html>
转我的贴,写你的名,是一种可耻的行为!请不要让我BS你!
阅读全文 | 回复(0) | 引用通告 | 编辑

发表评论:

    密码:
    主页:
    标题:
    页面数据正在载入...
<<  < 2007 - 12 >  >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Placard
1,長江後浪推前浪,前浪死在沙灘上!
2,有了钱的男人,才是真正的男人...
3,不要以为把你的小JJ埋进土里,你就rape了整个地球;不要以为把你的小JJ朝向天空,你就rape了整个宇宙!
Logon System
Search
Info about this blog
Others
myDream
bxna 京ICP备05002321号