zoukankan      html  css  js  c++  java
  • JavaWeb--自定义标签3--带标签体的自定义标签

    3)中

    package tagTest;
    
    import javax.servlet.jsp.JspContext;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.BodyContent;
    import javax.servlet.jsp.tagext.JspFragment;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    import java.io.IOException;
    import java.io.StringWriter;
    
    public class TestJspFragment extends SimpleTagSupport {
        private String time;
    
        public void setTime(String time) {
            this.time = time;
        }
    
        @Override
        public void doTag() throws JspException, IOException {
            //super.doTag();
            JspFragment bodyContent = getJspBody();
    
    
            StringWriter stringWriter = new StringWriter();
            bodyContent.invoke(stringWriter);
    
            String context = stringWriter.toString().toUpperCase();
    
            int count = Integer.parseInt(time);
            for(int i = 0; i < count; ++i) {
                getJspContext().getOut().print(i + 1 + ": " + context + "<br>");
            }
    
        }
    
    }
    

      

        <tag>
            <name>printWrite</name>
            <tag-class>tagTest.TestJspFragment</tag-class>
            <body-content>scriptless</body-content>
    
            <attribute>
                <name>time</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
    
        </tag>
    

      

    <skye:printWrite time="10">helloWorld</skye:printWrite>
    

      

    1.ForEachTag.java 类

    package tagTest;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    import java.io.IOException;
    import java.util.Collection;
    
    public class FotEachTag extends SimpleTagSupport {
    
        private Collection<?> collection;
        private String var;
    
        public Collection<?> getCollection() {
            return collection;
        }
    
        public void setCollection(Collection<?> collection) {
            this.collection = collection;
        }
    
        public String getVar() {
            return var;
        }
    
        public void setVar(String var) {
            this.var = var;
        }
    
        @Override
        public void doTag() throws JspException, IOException {
            if(collection != null){
                for(Object obj : collection){
                    getJspContext().setAttribute(var, obj);
                    getJspBody().invoke(null);
    
                }
            }
        }
    }
    

      2.tld文件中

     <tag>
            <name>testForEach</name>
    
            <tag-class>tagTest.FotEachTag</tag-class>
            <body-content>scriptless</body-content>
    
            <attribute>
                <name>collection</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
    
            <attribute>
                <name>var</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>
    

      3.servlet.jsp

    <%@ page import="tagTest.Customer" %>
    <%@ page import="java.util.ArrayList" %>
    <%@ page import="java.util.List" %><%--
      Created by IntelliJ IDEA.
      User: Skye
      Date: 2017/12/13
      Time: 14:38
      To change this template use File | Settings | File Templates.
    --%>
    <%@ taglib prefix="sky" uri="http://skye/jsp/jstl/core"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
    
    <%
        List<Customer> customers = new ArrayList<Customer>();
        customers.add(new Customer(1, "AA", 23));
        customers.add(new Customer(2, "AB", 22));
        customers.add(new Customer(3, "AC", 21));
        customers.add(new Customer(4, "AD", 20));
        customers.add(new Customer(5, "AE", 28));
        customers.add(new Customer(6, "AF", 26));
        customers.add(new Customer(8, "sa", 89));
    
        request.setAttribute("customers", customers);
    %>
    <sky:testForEach collection="${requestScope.customers}" var="cust">${cust.id} -- ${cust.name}<br></sky:testForEach>
    </body>
    </html>
    

      

  • 相关阅读:
    【模板时间】◆模板·III◆ 单调子序列
    【学时总结】◆学时·VII◆ 高维DP
    【例题收藏】◇例题·IV◇ Wooden Sticks
    【赛时总结】◇赛时·VI◇ Atcoder ABC-104
    【例题收藏】◇例题·III◇ 木と整数 / Integers on a Tree
    【学时总结】◆学时·VI◆ SPLAY伸展树
    【模板时间】◆模板·II◆ 树链剖分
    【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3
    【例题收藏】◇例题·II◇ Berland and the Shortest Paths
    【例题收藏】◇例题·I◇ Snuke's Subway Trip
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8056754.html
Copyright © 2011-2022 走看看