JSP简单标签带属性开发

1、开发带属性的标签,标签处理器类中属性要有相应setter方法,符合javaBean规范

2、tld文件中进行相应属性标签配置

属性配置相关说明如下图

3、简单标签带属性的例子

1)、通过设置标签属性,自定义循环标签体次数;基本数据类型,JSP页面传递字符串时可以自动转换成相应类型,复合数据类型就不能自动转换,如Date

package TagDemo;

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

public class SimpleTagWithAttrDemo extends SimpleTagSupport {
    private int count;

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment fragment = this.getJspBody();
        for (int i = 0; i < this.count; i++) {
            fragment.invoke(null);
        }
    }
}
带属性的自定义简单标签
<?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>自定义标签</description>

    <tlib-version>1.0</tlib-version>

    <short-name>SDT</short-name>

    <uri>/sefDefineTag</uri>
    <tag>
        <name>tagDemo</name>
        <tag-class>TagDemo.TagDemo</tag-class>
        <body-content>JSP</body-content>
    </tag>
    <tag>
        <description>测试循环</description>
        <name>iteration</name>
        <tag-class>TagDemo.IterationTagDemo</tag-class>
        <body-content>JSP</body-content>
    </tag>
    <tag>
        <description>测试修改标签体内容</description>
        <name>bodytag</name>
        <tag-class>TagDemo.BodyTagDemo</tag-class>
        <body-content>JSP</body-content>
    </tag>
    <tag>
        <name>simpleTag</name>
        <tag-class>TagDemo.SimpleTagDemo</tag-class>
        <!--简单标签中,body-content中一般设置成empty或scriptless,如果设置成JSP会抛异常;
        传统标签中,body-content中一般设置成empty或JSP;
        如果body-content设置成tagdependent,那么标签体里的内容是给标签处理类使用的,
        例如:标签体里的SQL语句,就可以设置成tagdependent,标签处理器类得到SQL语句处理-->
        <body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>simpleIterationTag</name>
        <tag-class>TagDemo.SimpleIterationTagDemo</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>simpleBodyTag</name>
        <tag-class>TagDemo.SimpleBodyTagDemo</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>simpleEndPageTag</name>
        <tag-class>TagDemo.SimpleEndPageTagDemo</tag-class>
        <body-content>empty</body-content>
    </tag>
    <tag>
        <name>simpleTagAttr</name>
        <tag-class>TagDemo.SimpleTagWithAttrDemo</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <description>标签处理器类count属性值</description>
            <name>count</name>
            <required>true</required>
            <!--rtexprvalue用来指示属性值是否可以是一个表达式-->
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
tld文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="sdt" uri="/sefDefineTag" %>
<html>
<head>
    <title></title>
</head>
<body>
<h1>下面是自定义标签:</h1>
<h6>---------------------------------------------</h6>

<sdt:tagDemo><%="java代码"%>标签体</sdt:tagDemo>
<br/>
<br/>
<sdt:iteration>循环标签体5次<br/></sdt:iteration>
<br/>
<br/>
<sdt:bodytag>abc</sdt:bodytag>
<br/>
<br/>
<sdt:simpleTag>简单标签体</sdt:simpleTag>
<br/>
<br/>
<sdt:simpleIterationTag>简单标签体循环5次<br/></sdt:simpleIterationTag>
<br/>
<br/>
<sdt:simpleBodyTag>abccde</sdt:simpleBodyTag>
<br/>
<br/>
<%--<sdt:simpleEndPageTag></sdt:simpleEndPageTag>--%>
<br/>
<br/>
<!--tld文件标签属性rtexprvalue为true时,属性表达式写法嵌套java代码如,count="<%=2+3%>"-->
<sdt:simpleTagAttr count="5">属性标签体循环<br/></sdt:simpleTagAttr>
<h6>---------------------------------------------</h6>

<div>
    <h1>自定义标签下面内容</h1>
</div>
</body>
</html>
JSPDemo

2)、标签属性是复合数据类型Date

package TagDemo;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleTagWithAttrDemo2 extends SimpleTagSupport {

    private Date date;

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext = (PageContext) this.getJspContext();
        DateFormat format=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        pageContext.getOut().write(format.format(date));
    }
}
Date属性自定义标签处理器类
<?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>自定义标签</description>

    <tlib-version>1.0</tlib-version>

    <short-name>SDT</short-name>

    <uri>/sefDefineTag</uri>
    <tag>
        <name>tagDemo</name>
        <tag-class>TagDemo.TagDemo</tag-class>
        <body-content>JSP</body-content>
    </tag>
    <tag>
        <description>测试循环</description>
        <name>iteration</name>
        <tag-class>TagDemo.IterationTagDemo</tag-class>
        <body-content>JSP</body-content>
    </tag>
    <tag>
        <description>测试修改标签体内容</description>
        <name>bodytag</name>
        <tag-class>TagDemo.BodyTagDemo</tag-class>
        <body-content>JSP</body-content>
    </tag>
    <tag>
        <name>simpleTag</name>
        <tag-class>TagDemo.SimpleTagDemo</tag-class>
        <!--简单标签中,body-content中一般设置成empty或scriptless,如果设置成JSP会抛异常;
        传统标签中,body-content中一般设置成empty或JSP;
        如果body-content设置成tagdependent,那么标签体里的内容是给标签处理类使用的,
        例如:标签体里的SQL语句,就可以设置成tagdependent,标签处理器类得到SQL语句处理-->
        <body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>simpleIterationTag</name>
        <tag-class>TagDemo.SimpleIterationTagDemo</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>simpleBodyTag</name>
        <tag-class>TagDemo.SimpleBodyTagDemo</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>simpleEndPageTag</name>
        <tag-class>TagDemo.SimpleEndPageTagDemo</tag-class>
        <body-content>empty</body-content>
    </tag>
    <tag>
        <name>simpleTagAttr</name>
        <tag-class>TagDemo.SimpleTagWithAttrDemo</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <description>标签处理器类count属性值</description>
            <name>count</name>
            <required>true</required>
            <!--rtexprvalue用来指示属性值是否可以是一个表达式-->
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>simpleTagAttr2</name>
        <tag-class>TagDemo.SimpleTagWithAttrDemo2</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>date</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
tld文件
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="sdt" uri="/sefDefineTag" %>
<html>
<head>
    <title></title>
</head>
<body>
<h1>下面是自定义标签:</h1>
<h6>---------------------------------------------</h6>

<sdt:tagDemo><%="java代码"%>标签体</sdt:tagDemo>
<br/>
<br/>
<sdt:iteration>循环标签体5次<br/></sdt:iteration>
<br/>
<br/>
<sdt:bodytag>abc</sdt:bodytag>
<br/>
<br/>
<sdt:simpleTag>简单标签体</sdt:simpleTag>
<br/>
<br/>
<sdt:simpleIterationTag>简单标签体循环5次<br/></sdt:simpleIterationTag>
<br/>
<br/>
<sdt:simpleBodyTag>abccde</sdt:simpleBodyTag>
<br/>
<br/>
<%--<sdt:simpleEndPageTag></sdt:simpleEndPageTag>--%>
<br/>
<br/>
<!--tld文件标签属性rtexprvalue为true时,属性表达式写法嵌套java代码如,count="<%=2+3%>"-->
<sdt:simpleTagAttr count="5">属性标签体循环<br/></sdt:simpleTagAttr>
<br/>
<br/>
<%
    Date now = new Date();
    request.setAttribute("date", now);
%>
<sdt:simpleTagAttr2 date="${date}"></sdt:simpleTagAttr2>
<br/>
<%--或者这样写:--%>
<sdt:simpleTagAttr2 date="<%=new Date()%>"></sdt:simpleTagAttr2>
<h6>---------------------------------------------</h6>

<div>
    <h1>自定义标签下面内容</h1>
</div>
</body>
</html>
JSPDemo
原文地址:https://www.cnblogs.com/hujiapeng/p/5791506.html