自定义标签的使用(总结)

引入jstl.jar、standard.jar

一、最简单的自定义标签()

1、编写继承SimpleTagSupport

public class MyTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        getJspContext().getOut().write("hello walker");
    }
}

2、表现tld文件,放在WEB-INF下

<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_0.xsd"
    version="2.0">
    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>SimpleTagLibrary</short-name>
    <uri>mytid</uri>

    <tag>
        <description>Outputs a colored tile</description>
        <name>hello</name>
        <tag-class>Test.MyTag</tag-class>
        <body-content>empty</body-content>
    </tag>

</taglib>

注:属性说明

  • description //描述信息
  • tlib-version //指定标签库的版本号,基本不用我们操心
  • short-name //指定标签库的短名字,也是没什么用
  • uri //这是一个重要的子元素,是该标签库的唯一标识
  • tag //看名字就知道,这是定义标签的子元素,很重要 

   tag标签的属性说明

  • description //描述信息
  • name //该标签的唯一标识,很重要
  • tag-class //指定了处理该标签的类,也就是使用该标签谁给我返回结果
  • body-content //标签体,后面详说,很重要
  • attribute //属性,后面介绍,很重要

3、调用

<%@taglib uri="mytid" prefix="SimpleTagLibrary" %>  

<mytags:hello />

 

二、带属性的自定义标签

  1、自定义标签类的编写

public class MyTag extends SimpleTagSupport {

    private String map;
    public String getMap(){
        return this.map;
    }
    public void setMap(String map){
        this.map = map;
    }
    @Override
    public void doTag() throws JspException, IOException {
        HashMap<String,Integer> maps = (HashMap<String,Integer>)(getJspContext().getAttribute(map));
        Object[] array = maps.keySet().toArray();

        for (String str : maps.keySet()){
            getJspContext().getOut().write("<tr>");
            getJspContext().getOut().write("<td>");
            getJspContext().getOut().write(str);
            getJspContext().getOut().write("</td>");
            getJspContext().getOut().write("<td>");
            getJspContext().getOut().write(""+maps.get(str));
            getJspContext().getOut().write("</td>");
            getJspContext().getOut().write("</tr>");
        }
    }
}

2、tld

 

<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_0.xsd"
    version="2.0">
    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>SimpleTagLibrary</short-name>
    <uri>mytid</uri>

    <tag>
        <description>Outputs a colored tile</description>
        <name>hello</name>
        <tag-class>Test.MyTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>map</name>
            <required>true</required>
            <fragment>true</fragment>
        </attribute>
    </tag>

</taglib>

注:

required:该属性是否必须

rtexprvalue:当前属性是否可接受运行时表达式的动态值

3、使用

<%
    HashMap<String,Integer> maps = new HashMap<String, Integer>();
    maps.put("李四",53);
    maps.put("张三",23);
    maps.put("walker",22);
    pageContext.setAttribute("map",maps);
  %>
      <table>
        <mytag:hello map="map"/>
      </table>

 

三、带标签体自定标签

 1、自定义标签类

/**
 * 带标签体的标签
 * @author Administrator
 *
 */
public class IteratorTag extends SimpleTagSupport{
    //标签属性,省去getter,setter
    private String collection;
    private String item;

    @Override
    public void doTag() throws JspException, IOException {
        //从page scope中获取属性名为collection的集合
        Collection itemList=(Collection) getJspContext().getAttribute(collection);
        //遍历集合
        for (Object object : itemList) {
            //将集合的元素设置到page范围
            getJspContext().setAttribute(item,object);
            //输出标签体
            getJspBody().invoke(null);
        }
    }
}

2、tld

<description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>myTaglib</short-name>
    <uri>http://www.chinaebi.org/myTaglib</uri>
    <tag>
        <name>iterator</name>
        <tag-class>com.chinaebi.test.IteratorTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>collection</name>
            <required>true</required>
            <fragment>true</fragment>
        </attribute>
        <attribute>
            <name>item</name>
            <required>true</required>
            <fragment>true</fragment>
        </attribute>
    </tag>

注:body-content说明

  • tagdependent:指定标签处理类自己负责处理标签体。
  • empty:指定该标签只能作用空标签使用。
  • scriptless:指定该标签的标签体可以是静态 HTML 元素,表达式语言,但不允许出现 JSP 脚本。
  • JSP:指定该标签的标签体可以使用 JSP 脚本。

3、使用

<%
    List list= new ArrayList();
    list.add("java");
    list.add("c++");
    list.add("php");
    pageContext.setAttribute("list",list);
    %>
    <table border="1" cellpadding="0" cellspacing="0">
    <myTag:iterator item="item" collection="list">
        <tr>
            <td>${item}</td>
        </tr>
    </myTag:iterator>
    </table>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值