---恢复内容开始---

1.定制标签简介

 JSP表示层中需要编写大量的jsp脚本代码

 在其他jsp中要重复编写相同的代码,不能做到代码的复用

JSP开发者可以创建新的标签来根据实际需求定制相应的方法----定制标签

 使用自定义标签可以实现页面表示层的代码重用

 几种常见的标签:

 空标签体的定制标签

<prefix:tagName></prefix:tagName>
<prefix:tagName />
例如
<mytag:required></mytag:required>
<mytag:required />

带属性的定制标签:

   -定制标签可以包含属性

   -<前缀:标签名 属性1= “值1” 属性2=“值2”/>

   例如:-<test:function user="join" num="100"/>

自定义标签:

 自定义简单标签   自定义标准标签  自定义JSP标签文件

2.简单标签简介及使用

 简单标签的概念:

JSP2.0提供了简单标签来快速实现自定义标签功能
简单标签提供了SimpleTag接口和SimpleTagSupport实现类

 自定义简单标签的步骤: 

编写简单标签实现类
编写TLD文件
在web.xml中配置TLD文件
在JSP页面中使用taglib

 (1)编写简单标签实现类

    

public  class SimpleTagExample
        extends SimpleTagSupport{
      public void doTag()   throws
                     JspException,IOException
      {
           getJspContext().getOut().print(“simple tag!”);
       }
}

  (2)编写标签库描述符文件 

标签库描述符文件包含的信息可以指导JSP容器解析JSP的定制标签
一个标签库描述符就是一个XML文档,用于通知标签库使用者关于标签库的功能和用法

<?xml version=“1.0” encoding=“utf-8” ?>
<taglib><tag>
    <name>…  </name>
    <tag-class>…  </tag-class>
    <body-content>…  </body-content>
    <attribute>      …    </attribute>
  </tag>
</taglib>
<taglib>
    <uri>http://localhost:8080/08-03/math</uri>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <tag>
        <name>sqrt</name>
        <tag-class>com.MathTag01</tag-class>
        <body-content>empty</body-content>
        <description>
            Send a math expression to the JSP
        </description>
    </tag>
</taglib>

 (3)在web.xml中配置TLD文件

<jsp-config> JSP相关配置
<taglib> 定义在JSP中可使用的库
<taglib-uri>定义TLD文件的URI,JSP页面的tablib命令可以经由此URI获取到TLD文件
<taglib-location> TLD文件所在的位置

<jsp-config>
      <taglib>
    <taglib-uri>http://localhost:8080/08-03/math</taglib-uri>
    <taglib-location>/MathTag01.tld</taglib-location>
      </taglib>
</jsp-config>

   (4)在JSP页面中使用taglib

<%@taglib prefix=“math” uri=“..../math"%>
…
    <math:sqrt />

简单标签实现方法:

public  class SimpleTagExample
        extends SimpleTagSupport{
      public void doTag()   throws
                     JspException,IOException
      {
           getJspContext().getOut().print(“simple tag!”);
       }
}
10-08 15:25