zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVA开发JSP-Servlet学习笔记:自定义标签-编写带标签体的标签

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package lee;
    
    import javax.servlet.jsp.tagext.*;
    import javax.servlet.jsp.*;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    
    /**
     *
     * @author Administrator
     */
    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);
            }
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.1" 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">
        <tlib-version>1.0</tlib-version>
        <short-name>mytaglib</short-name>
        <uri>/WEB-INF/tlds/mytaglib</uri>
        <!-- A validator verifies that the tags are used correctly at JSP
              translation time. Validator entries look like this: 
           <validator>
               <validator-class>com.mycompany.TagLibValidator</validator-class>
               <init-param>
                  <param-name>parameter</param-name>
                  <param-value>value</param-value>
               </init-param>
           </validator>
        -->
        <!-- A tag library can register Servlet Context event listeners in
             case it needs to react to such events. Listener entries look
             like this: 
          <listener>
              <listener-class>com.mycompany.TagLibListener</listener-class> 
          </listener>
        -->
        <!-- 定义第一个标签 -->
        <!-- 定义第一个标签 -->
        <tag>
            <!-- 定义标签名 -->
            <name>helloWorld</name>
            <!-- 定义标签处理类 -->
            <tag-class>lee.HelloWorldTag</tag-class>
            <!-- 定义标签体为空 -->
            <body-content>empty</body-content>
        </tag>
    
        <!-- 定义第二个标签 -->
        <tag>
            <!-- 定义标签名 -->
            <name>query</name>
            <!-- 定义标签处理类 -->
            <tag-class>lee.QueryTag</tag-class>
            <!-- 定义标签体为空 -->
            <body-content>empty</body-content>
            <!-- 配置标签属性:driver -->
            <attribute>
                <name>driver</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:url -->
            <attribute>
                <name>url</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:user -->
            <attribute>
                <name>user</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:pass -->
            <attribute>
                <name>pass</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
            <!-- 配置标签属性:sql -->
            <attribute>
                <name>sql</name> 
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
        </tag>
    
        <!-- 定义第三个标签 -->
        <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>
        <tag>
            <!-- 定义标签名 -->
            <name>fragment</name>
            <!-- 定义标签处理类 -->
            <tag-class>lee.FragmentTag</tag-class>
            <!-- 指定该标签不支持标签体 -->
            <body-content>empty</body-content>
            <!-- 定义标签属性:fragment -->
            <attribute>
                <name>fragment</name>
                <required>true</required>
                <fragment>true</fragment>
            </attribute>
        </tag>
        <!-- 定义接受动态属性的标签 -->
        <tag>
            <name>dynaAttr</name>
            <tag-class>lee.DynaAttributesTag</tag-class>
            <body-content>empty</body-content>
            <!-- 指定支持动态属性 -->
            <dynamic-attributes>true</dynamic-attributes>
        </tag>
    </taglib>
    <%-- 
        Document   : iteratorTag
        Created on : 2020-5-4, 23:29:17
        Author     : Administrator
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@ page import="java.util.*"%>
    <%@ taglib uri= "/WEB-INF/tlds/mytaglib" prefix="mytag"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title> 带标签体的标签-迭代器标签 </title>
        </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>

  • 相关阅读:
    (转)【web前端培训之前后端的配合(中)】继续昨日的故事
    ural(Timus) 1136. Parliament
    scau Josephus Problem
    ACMICPC Live Archive 6204 Poker End Games
    uva 10391 Compound Words
    ACMICPC Live Archive 3222 Joke with Turtles
    uva 10132 File Fragmentation
    uva 270 Lining Up
    【转】各种字符串哈希函数比较
    uva 10905 Children's Game
  • 原文地址:https://www.cnblogs.com/tszr/p/12828895.html
Copyright © 2011-2022 走看看