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>

  

 

posted @ 2017-12-18 11:35  SkyeAngel  阅读(191)  评论(0编辑  收藏  举报