`
hai0378
  • 浏览: 517028 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JSP标签开发--详解

 
阅读更多

标签开发之几大步骤:

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

package org.lxh.tagdemo;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport{
	public int doStartTag()throws JspException{
	 JspWriter out =super.pageContext.getOut();
	 try{
         out.println("<h1>hello World!!</h1>");
	 }catch(Exception e){}
     return TagSupport.SKIP_BODY;
	}
};

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

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

<?xml version="1.0" encoding="UTF-8"?>
<taglib 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-jsptaglibrary_2_1.xsd"
    version="2.1">
<tlib-version>1.0<tlib-version>  <!--表示标签库的版本-->
<short-name>firsttag</short-name> <!--为标签库在TLD中的描述名称-->
<tag>
   <name>hello</name>
   <tag-class>org.lxh.tagdemo.HelloTag</tag-class>
   <body-content>empty</body-content>
</tag>
</taglib>

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

 

4,标签组成部分

 

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

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

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

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

 

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

1,定义有属性的标签

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

package org.lxh.tagdemo;
import java.text.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class DateTag extends TagSupport{
    private String format;  //设置属性的时候可以通过setter完成
	public int doStartTag()throws JspException{
	 SimpleDateFormat sdf = new SimpleDateFormat(this.format);
	 //表示进行格式化的日期显示操作
	 try{
	 	super.pageContext.getOut().write(sdf.format(new Date()));
	 }catch (Exception e){
        e.printStackTrace();
	 }
	 
	 return TagSupport.SKIP_BODY;
	}
    public void setFormat(String format){
	  this.format = format;
	}
	public String getFormat(){
	  return this.format;
	}
};
 

2,编写 *.tld文件,

<?xml version="1.0" encoding="UTF-8"?>
<taglib 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-jsptaglibrary_2_1.xsd"
    version="2.1">
<tlib-version>1.0<tlib-version>  <!--表示标签库的版本-->
<short-name>datetag</short-name> <!--为标签库在TLD中的描述名称-->
<tag>
   <name>date</name>
   <tag-class>org.lxh.tagdemo.DateTag</tag-class>
   <body-content>empty</body-content>
   <attribute>    <!-- 定义属性 -->
          <name>format</name>
	  <required>true</required>
	  <rtexprvalue>true</rtexprvalue><!-- 支持表达式输出 -->
   </attribute>
</tag>
</taglib>
 

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

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  <jsp-config>
  	<taglib>
	  <taglib-uri>mldn_date</taglib-uri>
	  <taglib-location>/WEB-INF/datetag.tld</taglib-location>
	</taglib>
  </jsp-config>

 </web-app>

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

 

TagSupport类的简单介绍:

1,

doStartTag ()

2,

int doAfterBody ()
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 int SKIP_PAGE

 

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

 

static int EVAL_PAGE

 

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

 

1.1 编写 *.java 文件:

package org.lxh.tagdemo;
import java.text.*;
import java.util.*;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class AttributeTag extends TagSupport{
   private String name;
   private String scope;

   public int doStartTag() throws JspException{
      //判断属性是否存在
	  Object value=null;
	  if("page".equals(this.scope)){  //是否是page范围
	     value = super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE);
	  }
	  if("request".equals(this.scope)){  //是否是page范围
	     value = super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE);
	  }
	  if("session".equals(this.scope)){  //是否是page范围
	     value = super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE);
	  }
	  if("application".equals(this.scope)){  //是否是page范围
	     value = super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);
	  }
	  if(value == null){
	    return TagSupport.SKIP_BODY;  //没有属性不执行标签体
	  } else {
	    return TagSupport.EVAL_BODY_INCLUDE ;  //执行标签体
	  }
   }
   public void setName(String name){
     this.name  = name;
   }
   public void setScope(String scope){
     this.scope  =scope;
   }
   public String getName(){
     return this.name;
   }
   public String getScope(){
     return this.scope;
   }
};
 

1.2, 编写 *.tld文件

<?xml version="1.0" encoding="UTF-8"?>
<taglib 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-jsptaglibrary_2_1.xsd"
    version="2.1">
<tlib-version>1.0</tlib-version>  
<short-name>mldntag</short-name> 
<tag>
   <name>present</name>
   <tag-class>org.lxh.tagdemo.AttributeTag</tag-class>
   <body-content>JSP</body-content>
   <attribute>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>true<rtexprvalue>
   </attribute>
   <attribute>
      <name>scope</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
   </attribute>
</tag>
</taglib>

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

 

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  <jsp-config>
  	<taglib>
	  <taglib-uri>mldn_date</taglib-uri>
	  <taglib-location>/WEB-INF/datetag.tld</taglib-location>
	</taglib>
	<taglib>
	  <taglib-uri>mldn_page</taglib-uri>
	  <taglib-location>/WEB-INF/mldntag.tld</taglib-location>
	</taglib>
  </jsp-config>

 </web-app>

 1.4,开始写页面

 

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="mytag" uri="mldn_page"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<h1>
  <%
    String scope = "session";
	session.setAttribute("username","zhangsan");
  %>
 <mytag:present name="username" scope="<%=scope%>">
 <h2><%=scope%>范围存在属性,内容是:"${sessionScope.username}"</h2>
 </mytag:present>
 <mytag:present name="allusers" scope="request">
 <h2><%=scope%>范围存在属性,内容是:"${requestScope.allusers}"</h2>
 </mytag:present>
</h1>
</body>
</html>

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

 

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

                             BodyTagSupport类的定义如下:

 public class BodyTagSupport extends TagSupport implements BodyTag

 

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

 

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

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    jsp应用开发详解.part1

    jsp应用开发详解.part1 因为上传文件大小受限part2请到我的资源下载 作者:作者:刘晓华//张健//周慧贞 译者:作者:刘晓华//张健//周慧贞 出版社:电子工业出版社 本书结合JSP和Servlet的最新规范,从基本的...

    jsp应用开发详解 刘晓华

    jsp应用开发详解.part2 因为上传文件大小受限part1请到我的资源下载 作者:作者:刘晓华//张健//周慧贞 译者:作者:刘晓华//张健//周慧贞 出版社:电子工业出版社 本书结合JSP和Servlet的最新规范,从基本的...

    Jsp基础与案例开发详解随书源码13-17章

    该资源是《JSP基础与案例开发详解》的随书源代码。提供了书中从第一章-第十七章中讲解的开发工具和源代码。 目录如下: 第1章:Jsp开发的基本知识以及一些常用软件的安装和配置。 第2章:网页布局与修饰,为前端开发...

    JSP基础与案例开发详解 光盘part1

    分为两部分 内容推荐 本书以Java为平台,结合应用实例,强调“实用”性,系统、全面地介绍了JSP语言的基础知识及应用方向。...特性》、《JavamB件开发详解》等一系列Java畅销书籍,发布了中国第一套Java学习视频

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part2

    本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。.  本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...

    JSP基础与案例开发详解随书源码2-10章

    该资源是《JSP基础与案例开发详解》的随书源代码。提供了书中从第一章-第十七章中讲解的开发工具和源代码。 目录如下: 第1章:Jsp开发的基本知识以及一些常用软件的安装和配置。 第2章:网页布局与修饰,为前端开发...

    jsp应用开发详解.pdf

    一个详细的JSP开发应用文档,包含表达式、标签操作等相关实例和解说,可谓JSP开发必备资料

    jsp自定义标签开发+TLD文件元素详解+实例

    jsp自定义标签开发+TLD文件元素详解+实例;从jsp1.1开始就可以在jsp页面中使用自定义标签了,使用自定义标签不但可以实现代码重用,而且可以使jsp代码更简洁。Jsp2.0的标签扩展API中又增加了SimpleTag接口和其实现类...

    JSP基础与案例开发详解配书源码(全部)

    该资源是《JSP基础与案例开发详解》的随书源代码。提供了书中从第一章-第十七章中讲解的开发工具和源代码。 目录如下: 第1章:Jsp开发的基本知识以及一些常用软件的安装和配置。 第2章:网页布局与修饰,为前端...

    JSP应用开发详解(第三版)

    资源名称:JSP应用开发详解(第三版)内容简介:本书结合JSP和Servlet的最新规范,从基本的语法和规范入手,以经验为后盾,以实用为目标,以实例为导向,以实践为指导,深入浅出地讲解了JSP开发中的种种问题。...

    JSP基础与案例开发详解 光盘part2

    分为两部分 内容推荐 本书以Java为平台,结合应用实例,强调“实用”性,系统、全面地介绍了JSP语言的基础知识及应用方向。...特性》、《JavamB件开发详解》等一系列Java畅销书籍,发布了中国第一套Java学习视频

    Jsp基础与案例开发详解随书源码11-12章

    该资源是《JSP基础与案例开发详解》的随书源代码。提供了书中从第一章-第十七章中讲解的开发工具和源代码。 目录如下: 第1章:Jsp开发的基本知识以及一些常用软件的安装和配置。 第2章:网页布局与修饰,为前端开发...

    JSP应用开发详解(第二版)

    书附带光盘源代码,【内容提要】 飞思科技:本书分7篇共25章,包括JSP应用开发基础、JSP核心技术、Servlet技术的应用、数据库技术的使用、标签语言和表达式语言、Web框架的使用、JSP在实际开发中的应用。

    软件开发JSP基础与实例(张孝祥)源码

    读者注意:这套视频语音讲解中引述的《软件开发课堂 - Java Web基础与案例开发详解》,即后来改名为《JSP基础与案例开发详解》一书的原稿,二者是同一本书。 01基础案例 02网页布局和修饰 03JDBC的应用 04Servlet的...

    JSTL_标签库详解大全JSTL常用标签汇总java开发jsp开发.pdf

    JSTL_标签库详解大全JSTL常用标签汇总java开发jsp开发.pdf

    JSTL_标签库详解大全JSTL常用标签汇总java开发jsp开发归类.pdf

    JSTL_标签库详解大全JSTL常用标签汇总java开发jsp开发归类.pdf

    JSP应用开发详解

    以精简的内容介绍了JSP的语法、Servlet技术、JDBC技术、标签库技术、表达式语言、Struts、JavaServerFace、SpringMVC、Hibernate、Ajax等技术;对于JSP开发中常遇到的典型难点,专门设置了专题进行集中讨论。对于...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part3

    本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。.  本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...

    jsp应用开发详解

    以精简的内容介绍了JSP的语法、Servlet技术、JDBC技术、标签库技术、表达式语言、Struts、JavaServerFace、SpringMVC、Hibernate、Ajax等技术;对于JSP开发中常遇到的典型难点,专门设置了专题进行集中讨论。对于...

Global site tag (gtag.js) - Google Analytics