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

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"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title heretitle>
<%
   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



<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.0tlib-version>
    <short-name>pleaseTagshort-name>


    <tag>
       <name>iteratename>
       <tag-class>
          com.lang.tag.IterateTag
       tag-class>
       <body-content>JSPbody-content>
       <attribute>
         <name>varname>
         <required>truerequired>
         <rtexprvalue>truertexprvalue> 
       attribute>
       <attribute>
         <name>itemsname>
         <required>truerequired>
         <rtexprvalue>truertexprvalue> 
       attribute>
    tag>

taglib>

运行结果:
第九章自定义标签第四节自定义有标签体的标签_第1张图片

你可能感兴趣的:(jsp/servlet)