JSP标签开发--详解

来源:互联网 发布:黑客引导页html源码 编辑:程序博客网 时间:2024/06/15 20:55

标签开发之几大步骤:

1,开发标签类,继承TagSupport类,

Java代码  收藏代码
  1. package org.lxh.tagdemo;  
  2. import javax.servlet.jsp.*;  
  3. import javax.servlet.jsp.tagext.*;  
  4. public class HelloTag extends TagSupport{  
  5.     public int doStartTag()throws JspException{  
  6.      JspWriter out =super.pageContext.getOut();  
  7.      try{  
  8.          out.println("<h1>hello World!!</h1>");  
  9.      }catch(Exception e){}  
  10.      return TagSupport.SKIP_BODY;  
  11.     }  
  12. };  

注意点 :编译的时候可能报错,请注意 tomcat路径下,jsp-api.jar的路径问题,把此文件夹放到 java环境下,jdk/jre/lib/ext 包下面,

2,定义一个标签的描述文件,标签的描述文件的后缀 "*.tld",而且此文件也符合XML语法规则。

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"  
  5.     version="2.1">  
  6. <tlib-version>1.0<tlib-version>  <!--表示标签库的版本-->  
  7. <short-name>firsttag</short-name> <!--为标签库在TLD中的描述名称-->  
  8. <tag>  
  9.    <name>hello</name>  
  10.    <tag-class>org.lxh.tagdemo.HelloTag</tag-class>  
  11.    <body-content>empty</body-content>  
  12. </tag>  
  13. </taglib>  

 3,在JSP文件之中使用此标签。

 

4,标签组成部分

 

 4.1>.标签处理类: HelloTag.java;

 4.2>.标签描述文件:hellotag.tlc

 4.3>.JSP页面: 通过<%@ taglib%>定义标签.

 4.4>(可选)在web.xml文件中配置映射名称.

 

********************************************************************************

1,定义有属性的标签

示例:自定义日期格式化操作

Java代码  收藏代码
  1. package org.lxh.tagdemo;  
  2. import java.text.*;  
  3. import java.util.*;  
  4. import javax.servlet.jsp.*;  
  5. import javax.servlet.jsp.tagext.*;  
  6. public class DateTag extends TagSupport{  
  7.     private String format;  //设置属性的时候可以通过setter完成  
  8.     public int doStartTag()throws JspException{  
  9.      SimpleDateFormat sdf = new SimpleDateFormat(this.format);  
  10.      //表示进行格式化的日期显示操作  
  11.      try{  
  12.         super.pageContext.getOut().write(sdf.format(new Date()));  
  13.      }catch (Exception e){  
  14.         e.printStackTrace();  
  15.      }  
  16.        
  17.      return TagSupport.SKIP_BODY;  
  18.     }  
  19.     public void setFormat(String format){  
  20.       this.format = format;  
  21.     }  
  22.     public String getFormat(){  
  23.       return this.format;  
  24.     }  
  25. };  
 

2,编写 *.tld文件,

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"  
  5.     version="2.1">  
  6. <tlib-version>1.0<tlib-version>  <!--表示标签库的版本-->  
  7. <short-name>datetag</short-name> <!--为标签库在TLD中的描述名称-->  
  8. <tag>  
  9.    <name>date</name>  
  10.    <tag-class>org.lxh.tagdemo.DateTag</tag-class>  
  11.    <body-content>empty</body-content>  
  12.    <attribute>    <!-- 定义属性 -->  
  13.           <name>format</name>  
  14.       <required>true</required>  
  15.       <rtexprvalue>true</rtexprvalue><!-- 支持表达式输出 -->  
  16.    </attribute>  
  17. </tag>  
  18. </taglib>  
 

3,为了方便的使用,直接在web.xml文件之中定义此标签库.

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!--  
  3.  Licensed to the Apache Software Foundation (ASF) under one or more  
  4.   contributor license agreements.  See the NOTICE file distributed with  
  5.   this work for additional information regarding copyright ownership.  
  6.   The ASF licenses this file to You under the Apache License, Version 2.0  
  7.   (the "License"); you may not use this file except in compliance with  
  8.   the License.  You may obtain a copy of the License at  
  9.   
  10.       http://www.apache.org/licenses/LICENSE-2.0  
  11.   
  12.   Unless required by applicable law or agreed to in writing, software  
  13.   distributed under the License is distributed on an "AS IS" BASIS,  
  14.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15.   See the License for the specific language governing permissions and  
  16.   limitations under the License.  
  17. -->  
  18.   
  19. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  20.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  21.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  22.    version="2.5">  
  23.   
  24.   <display-name>Welcome to Tomcat</display-name>  
  25.   <description>  
  26.      Welcome to Tomcat  
  27.   </description>  
  28.   <jsp-config>  
  29.     <taglib>  
  30.       <taglib-uri>mldn_date</taglib-uri>  
  31.       <taglib-location>/WEB-INF/datetag.tld</taglib-location>  
  32.     </taglib>  
  33.   </jsp-config>  
  34.   
  35.  </web-app>  

 ************************************************************************************

 

TagSupport类的简单介绍:

1,

 doStartTag() 

2,

intdoAfterBody() 
Default processing for a body.

<!-- Generated by javadoc (build 1.6.0_20) on Fri Jun 04 05:41:16 PDT 2010 -->

<noscript></noscript>

SKIP_BODY

static final int SKIP_BODY :

   表示表前提内容会被忽略,并且将执行权转交给 doEndTag()方法.

<!-- Generated by javadoc (build 1.6.0_20) on Fri Jun 04 05:41:16 PDT 2010 -->

<noscript></noscript>

EVAL_BODY_INCLUDE

static final int EVAL_BODY_INCLUDE

表示重复执行标签体的内容,会重复条用doAfterBody()方法,一直循环执行下去,直到doAfterBody()方法返回SKIP_BODY为止。

 

<!-- Generated by javadoc (build 1.6.0_20) on Fri Jun 04 05:41:16 PDT 2010 -->

<noscript></noscript>

doEndTag

int doEndTag()             throws JspException
static intSKIP_PAGE

 

表示JSP页面应该立刻停止执行,并将所有的输出立刻回传到浏览器上

 

static intEVAL_PAGE

 

表示JSP可以正常的运行完毕。

 

1.1 编写 *.java 文件:

Java代码  收藏代码
  1. package org.lxh.tagdemo;  
  2. import java.text.*;  
  3. import java.util.*;  
  4. import java.io.*;  
  5. import javax.servlet.jsp.*;  
  6. import javax.servlet.jsp.tagext.*;  
  7. public class AttributeTag extends TagSupport{  
  8.    private String name;  
  9.    private String scope;  
  10.   
  11.    public int doStartTag() throws JspException{  
  12.       //判断属性是否存在  
  13.       Object value=null;  
  14.       if("page".equals(this.scope)){  //是否是page范围  
  15.          value = super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE);  
  16.       }  
  17.       if("request".equals(this.scope)){  //是否是page范围  
  18.          value = super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE);  
  19.       }  
  20.       if("session".equals(this.scope)){  //是否是page范围  
  21.          value = super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE);  
  22.       }  
  23.       if("application".equals(this.scope)){  //是否是page范围  
  24.          value = super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);  
  25.       }  
  26.       if(value == null){  
  27.         return TagSupport.SKIP_BODY;  //没有属性不执行标签体  
  28.       } else {  
  29.         return TagSupport.EVAL_BODY_INCLUDE ;  //执行标签体  
  30.       }  
  31.    }  
  32.    public void setName(String name){  
  33.      this.name  = name;  
  34.    }  
  35.    public void setScope(String scope){  
  36.      this.scope  =scope;  
  37.    }  
  38.    public String getName(){  
  39.      return this.name;  
  40.    }  
  41.    public String getScope(){  
  42.      return this.scope;  
  43.    }  
  44. };  
 

1.2, 编写 *.tld文件

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"  
  5.     version="2.1">  
  6. <tlib-version>1.0</tlib-version>    
  7. <short-name>mldntag</short-name>   
  8. <tag>  
  9.    <name>present</name>  
  10.    <tag-class>org.lxh.tagdemo.AttributeTag</tag-class>  
  11.    <body-content>JSP</body-content>  
  12.    <attribute>  
  13.       <name>name</name>  
  14.       <required>true</required>  
  15.       <rtexprvalue>true<rtexprvalue>  
  16.    </attribute>  
  17.    <attribute>  
  18.       <name>scope</name>  
  19.       <required>true</required>  
  20.       <rtexprvalue>true</rtexprvalue>  
  21.    </attribute>  
  22. </tag>  
  23. </taglib>  

  1.3,在 WEB-INF/web.xml 文件里面配置标签属性,

 

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!--  
  3.  Licensed to the Apache Software Foundation (ASF) under one or more  
  4.   contributor license agreements.  See the NOTICE file distributed with  
  5.   this work for additional information regarding copyright ownership.  
  6.   The ASF licenses this file to You under the Apache License, Version 2.0  
  7.   (the "License"); you may not use this file except in compliance with  
  8.   the License.  You may obtain a copy of the License at  
  9.   
  10.       http://www.apache.org/licenses/LICENSE-2.0  
  11.   
  12.   Unless required by applicable law or agreed to in writing, software  
  13.   distributed under the License is distributed on an "AS IS" BASIS,  
  14.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15.   See the License for the specific language governing permissions and  
  16.   limitations under the License.  
  17. -->  
  18.   
  19. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  20.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  21.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  22.    version="2.5">  
  23.   
  24.   <display-name>Welcome to Tomcat</display-name>  
  25.   <description>  
  26.      Welcome to Tomcat  
  27.   </description>  
  28.   <jsp-config>  
  29.     <taglib>  
  30.       <taglib-uri>mldn_date</taglib-uri>  
  31.       <taglib-location>/WEB-INF/datetag.tld</taglib-location>  
  32.     </taglib>  
  33.     <taglib>  
  34.       <taglib-uri>mldn_page</taglib-uri>  
  35.       <taglib-location>/WEB-INF/mldntag.tld</taglib-location>  
  36.     </taglib>  
  37.   </jsp-config>  
  38.   
  39.  </web-app>  

 1.4,开始写页面

 

Java代码  收藏代码
  1. <%@ page contentType="text/html" pageEncoding="GBK"%>  
  2. <%@ taglib prefix="mytag" uri="mldn_page"%>  
  3. <html>  
  4. <head>  
  5.     <title>www.MLDNJAVA.cn</title>  
  6. </head>  
  7. <body>  
  8. <h1>  
  9.   <%  
  10.     String scope = "session";  
  11.     session.setAttribute("username","zhangsan");  
  12.   %>  
  13.  <mytag:present name="username" scope="<%=scope%>">  
  14.  <h2><%=scope%>范围存在属性,内容是:"${sessionScope.username}"</h2>  
  15.  </mytag:present>  
  16.  <mytag:present name="allusers" scope="request">  
  17.  <h2><%=scope%>范围存在属性,内容是:"${requestScope.allusers}"</h2>  
  18.  </mytag:present>  
  19. </h1>  
  20. </body>  
  21. </html>  

<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

BodyTagSupport : 是TagSupport类的子类,通过集成BodyTagSupport类实现的标签可以直接处理标签体内容的数

                             BodyTagSupport类的定义如下:

 public class BodyTagSupport extends TagSupport implements BodyTag

 

标签的开发在实际的开发中使用的很少,主要有第三方的提供的一些插件(重点 )

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 JSP 2.0 之后提供了 SimpleTagSupport 类,直接覆写里面的doTag()方法即可完成。

 

0 0