jsp自定义标签

要创建自定义的JSP标签,首先必须创建处理标签的Java类

1.自定义类并继承SimpleTagSupport

public class ObjToJson extends SimpleTagSupport {
    private Object obj;

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        if (null != obj) {
            out.write(JSON.toJSONString(obj));
        }
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

}

2.编写tld文件,并添加到WEB-INF下

<?xml version="1.0" encoding="UTF-8" ?>

<taglib 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-jsptaglibrary_2_1.xsd"
    version="2.1">

    <description>cnsdhzzl tagUtils</description>
    <display-name></display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>zl</short-name>
    <uri>http://cn.cnsdhzzl.tag</uri>

    <tag>
        <description>object to json string</description>
        <name>objToJson</name>
        <tag-class>cn.cnsdhzzl.utils.ObjToJson</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <description>Object</description>
            <name>obj</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

</taglib>

3.jsp页面引用

<%@ taglib uri="http://cn.cnsdhzzl.tag" prefix="zl"%>

<table border="1px;">
    <tr>
        <c:forEach items="${list }" var="i">
            <td>${i.id }</td>
            <td>${i.name }</td>
            <td>${i.age }</td>
        </c:forEach>
    </tr>
    <tr>
        <td colspan="6">${list }</td>
    </tr>
    <tr>
        <td colspan="6"><zl:objToJson obj="${list }"></zl:objToJson></td>
    </tr>
</table>

效果图

 

标签说明

<short-name>:指明推荐使用的prefix(这个属性会在jsp页面中引入标签的时候出现)。
<uri>:指明引用这个标签库时使用的uri(注意哦是uri不是url)。
<tag>:指明要定义标签的信息。

属性说明

name        定义属性的名称。每个标签的是属性名称必须是唯一的。
tag-class    指定映射的Java类。 required    指定属性是否是必须的或者可选的,如果设置为false为可选。 rtexprvalue  声明在运行表达式时,标签属性是否有效。 type     定义该属性的Java类类型 。默认指定为 String description  描述信息 fragment    如果声明了该属性,属性值将被视为一个 JspFragment。
bodycontent  指明标签体的限定,empty:指定该标签只能作用空标签使用;scriptless:指定该标签的标签体可以是静态HTML元素、表达式语言,但不允许出现JSP脚本。

SimpleTag接口详解:

方法

说明

doTag()方法

是核心方法,用于编写标签处理逻辑。

setParent(JspTag)方法

将父标签处理器对象传递给标签处理器。

getParent()方法

获取当前标签的父标签对象。

setJspContext(JspContext)方法

将JSP页面的pageContext对象传递给标签处理器对象。

setJspBody(JspFragment)方法

将代表当前标签体的JspFragment对象传递给标签处理器对象。(JspFragment中的invoke(Writer)方法用于执行JspFragment对象所代表的代码段)

body-content的值共有4种:

  <body-content>tagdependent</body-content>
  <body-content>JSP</body-content>
  <body-content>empty</body-content>
  <body-content>scriptless</body-content>

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

如下:

<test:myList>

select name,age from users

</test:myList>

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

<my:test>

<%=request.getProtocol()%> // ②

</my:test>

具体可参考后面附源码。

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

下面几种写法都是有效的,

<test:mytag />

<test:mytag uname="Tom" />

<test:mytag></test:mytag>

scriptless:接受文本、EL和JSP动作。如上述②使用<body-content> scriptless </body-content>则报错,具体可参考后面附源码。

猜你喜欢

转载自www.cnblogs.com/cnsdhzzl/p/8934476.html