欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第九章自定义标签第四节自定义有标签体的标签

程序员文章站 2022-06-09 09:18:02
...

IterateTag.jsp

<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <%@ taglib prefix="please" uri="/WEB-INF/lang.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<%
   List people = new ArrayList();
   people.add("王小二");
   people.add("思思光");
   people.add("*");
   pageContext.setAttribute("people", people);
%>
</head>
<body>
     <please:iterate items="people" var="p">
     <h2>${p }</h2>
     </please:iterate>

</body>
</html>

iterateTag.java

package com.lang.tag;

import java.util.List;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class IterateTag extends TagSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String var;
    private String items;
    private Iterator iter;


    public String getVar() {
        return var;
    }


    public void setVar(String var) {
        this.var = var;
    }


    public String getItems() {
        return items;
    }


    public void setItems(String items) {
        this.items = items;
    }


    public Iterator getIter() {
        return iter;
    }


    public void setIter(Iterator iter) {
        this.iter = iter;
    }


    @Override
    public int doStartTag() throws JspException {
        Object value = this.pageContext.getAttribute(items);
        if (value!=null && value instanceof List) {
            this.iter = ((List)value).iterator();
            if(iter.hasNext()) {
                this.pageContext.setAttribute(var, iter.next());
                return TagSupport.EVAL_BODY_INCLUDE; //执行标签体
            }else {
                return TagSupport.SKIP_BODY; //退出标签执行
            }
        }else {
            return TagSupport.SKIP_BODY;
        }

    }


    @Override
    public int doAfterBody() throws JspException {
        if(iter.hasNext()) {
        this.pageContext.setAttribute(var, iter.next());
        return TagSupport.EVAL_BODY_AGAIN; //在执行一次标签体
        }else {
            return TagSupport.SKIP_BODY; //退出标签
        }

}
}

lang.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib 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"
    version="2.1">
    <tlib-version>1.0</tlib-version>
    <short-name>pleaseTag</short-name>


    <tag>
       <name>iterate</name>
       <tag-class>
          com.lang.tag.IterateTag
       </tag-class>
       <body-content>JSP</body-content>
       <attribute>
         <name>var</name>
         <required>true</required>
         <rtexprvalue>true</rtexprvalue> 
       </attribute>
       <attribute>
         <name>items</name>
         <required>true</required>
         <rtexprvalue>true</rtexprvalue> 
       </attribute>
    </tag>

</taglib>

运行结果:
第九章自定义标签第四节自定义有标签体的标签

相关标签: 标签