Label combat development tagged body

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/chengqiuming/article/details/100566910

Development of a custom label

package lee;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
import java.sql.*;
import java.util.*;

public class IteratorTag extends SimpleTagSupport
{
    // 标签属性,用于指定需要被迭代的集合
    private String collection;
    // 标签属性,指定迭代集合元素,为集合元素指定的名称
    private String item;

    // collection的setter和getter方法
    public void setCollection(String collection)
    {
        this.collection = collection;
    }
    public String getCollection()
    {
        return this.collection;
    }
    // item的setter和getter方法
    public void setItem(String item)
    {
        this.item = item;
    }
    public String getItem()
    {
        return this.item;
    }
    // 标签的处理方法,标签处理类只需要重写doTag()方法
    public void doTag() throws JspException, IOException
    {
        // 从page scope中获取名为collection的集合
        Collection itemList = (Collection)getJspContext().
            getAttribute(collection);
        // 遍历集合
        for (Object s : itemList)
        {
            // 将集合的元素设置到page范围内
            getJspContext().setAttribute(item, s );
            // 输出标签体
            getJspBody().invoke(null);
        }
    }
}

Two established TLD file

<?xml version="1.0" encoding="GBK"?>

<taglib xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    <tlib-version>1.0</tlib-version>
    <short-name>mytaglib</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.crazyit.org/mytaglib</uri>

    <!-- 定义第三个标签 -->
    <tag>
        <!-- 定义标签名 -->
        <name>iterator</name>
        <!-- 定义标签处理类 -->
        <tag-class>lee.IteratorTag</tag-class>
        <!-- 定义标签体不允许出现JSP脚本 -->
        <body-content>scriptless</body-content>
        <!-- 配置标签属性:collection -->
        <attribute>
            <name>collection</name>
            <required>true</required>
            <fragment>true</fragment>
        </attribute>
        <!-- 配置标签属性:item -->
        <attribute>
            <name>item</name>
            <required>true</required>
            <fragment>true</fragment>
        </attribute>
    </tag>
 
</taglib>

Three using labels

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="java.util.*"%>
<!-- 导入标签库,指定mytag前缀的标签,
由http://www.crazyit.org/mytaglib的标签库处理 -->
<%@ taglib uri="http://www.crazyit.org/mytaglib" prefix="mytag"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 带标签体的标签-迭代器标签 </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
    <h2>带标签体的标签-迭代器标签</h2><hr/>
    <%
    //创建一个List对象
    List<String> a = new ArrayList<String>();
    a.add("疯狂Java");
    a.add("www.crazyit.org");
    a.add("www.fkit.org");
    //将List对象放入page范围内
    pageContext.setAttribute("a" , a);
    %>
    <table border="1" bgcolor="#aaaadd" width="300">
        <!-- 使用迭代器标签,对a集合进行迭代 -->
        <mytag:iterator collection="a" item="item">
        <tr>
            <td>${pageScope.item}</td>
        <tr>
        </mytag:iterator>
    </table>
</body>
</html>

Four test

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/100566910
Recommended