关于JSP的自定义标签

前两天学习了JSP的自定义标签 ,现在来记述下,加深印象。

一、创建自定义标签类

自定义标签类的创建方式有多种,不过还是继承SimpleTagSupport类比较简单,没必要重写那么多方法,只需要重写doTag方法,然后把逻辑处理写在doTag方法里。此处是我的三个自定义标签处理类:

ChooseTag 类:

package com.gao.simpleTag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class ChooseTag extends SimpleTagSupport {
    private boolean flag;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public boolean getFlag() {
        return flag;
    }

    @Override
    public void doTag() throws JspException, IOException {
        getJspBody().invoke(null);
    }
}

WhenTag类:

package com.gao.simpleTag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class WhenTag extends SimpleTagSupport {
    private boolean test  =false;

    public void setTest(boolean test) {
        this.test = test;
    }


    @Override
    public void doTag() throws JspException, IOException {
        if (test){
            getJspBody().invoke(null);
            ChooseTag chooseTag = (ChooseTag) getParent();
            chooseTag.setFlag(true);
        }
    }
}

OtherWiseTag类:

package com.gao.simpleTag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class OtherWiseTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        ChooseTag chooseTag = (ChooseTag) getParent();
        if (!chooseTag.getFlag()){
            getJspBody().invoke(null);
        }
    }
}

自定义标签类的父类SimpleTagSupport里的几个成员变量及其对应的set、get方法

第一个变量parentTag是用来获取父标签的;

第二个变量jspContext是类JspContext实例,JspContext类作为PageContext的父类可以用来获取JSP上下文的信息;

第三个变量jspBody是类JspFragment的实例,该实例可以作为jsp页面的对象,用来返回Jsp页面的一段符合Jsp语法规范的Jsp代码。JspFragment类提供了 public abstract void invoke( Writer out ) throws JspException, IOException

和public abstract JspContext getJspContext();两个 抽象方法。invoke()方法用来对标签内容在修改、或者其他操作后是否输出到页面上或者跳过执行(如果不调用该方法,则跳过标签体的内容,)。getJspContext()方法获取一个JspContext实例对象.

二、创建自定义标签配置文件(*.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_0.xsd"
        version="2.0">

    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>SimpleTag</short-name>
    <uri>/mySimpleTag</uri>
    <tag>
        <name>chooseTag</name>
        <tag-class>com.gao.simpleTag.ChooseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>

    <tag>
        <name>whenTag</name>
        <tag-class>com.gao.simpleTag.WhenTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>test</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

    <tag>
        <name>otherWiseTag</name>
        <tag-class>com.gao.simpleTag.OtherWiseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    </taglib>

该配置文件的格式如上,下面说明几个标签的含义:

1、<description> 对该标签库的描述(可自定义)

2、<tlib-version>该标签库的版本(可自定义)

3、<short-name>该标签库的缩略名(可自定义)

4、<uri> 该标签库的地址URI,该值在引用该标签时需要用到,所以值尽量有意义

5、<tag>对应标签库里的具体标签

     ① <name> 该标签的名称,该值在引用是需要使用

     ②<tag-class> 处理该标签的逻辑类,此处为处理类的全名引用地址

     ③<body-content> 内容部分,此处有几个可选值。empty、scrptless、JSP、tagdependment

           empty:空标记,即起始标记和结束标记之间没有内容。

           scriptless:接受文本、EL和JSP动作.不支持Java代码

           JSP:接受所有JSP语法,如定制的或内部的tag、scripts、静态HTML、脚本元素、JSP指令和动作。

           tagdependent:标签体内容直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释。

     ④<attribute>该标签允许自定义标签内添加元素,标签内包含几个子标签,如<name>、<required>、<rtexprvalue>、                               <type>、<fragment>,下面是官方API给的属性说明:

description     a description of the attribute

name            the name of the attribute

required        whether the attribute is required or optional

rtexprvalue     whether the attribute is a runtime attribute

type            the type of the attributes

fragment        whether this attribute is a fragment  

三、在Jsp页面引用及使用

   1、在Jsp页面头部对标签库进行引用,引用格式如下:

<%@taglib prefix="mySimpleTag" uri="/mySimpleTag" %>

其中uri对应我们的*.tld配置文件中的uri即可。

使用标签是方法如下:

<%@taglib prefix="mySimpleTag" uri="/mySimpleTag" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        pageContext.setAttribute("age",22);
    %>
    <mySimpleTag:chooseTag>
        <mySimpleTag:whenTag test="${age >= 18}">
            <h3>你已经成年了</h3>
        </mySimpleTag:whenTag>
        <mySimpleTag:otherWiseTag>
            <h3>小朋友,快快长大哦</h3>
        </mySimpleTag:otherWiseTag>
    </mySimpleTag:chooseTag>
</body>
</html>

运行结果如下:

记述:虽然该技术很早就已经出现了,不过到现在为止此技术仍经常被使用,希望以后当自己每次翻起此文章时能给自己点灵感和想法,也希望给刚使用次技术的新同学经验。

猜你喜欢

转载自blog.csdn.net/gaotiedun1/article/details/84235300