我们可以使用IterationTag接口doAfterBody()方法来迭代任何标签的体内容。

在这里,我们将使用实现IterationTag接口的TagSupport类。为了迭代体内容,我们需要在doAfterBody()方法中使用EVAL_BODY_AGAIN常量。

使用JSP自定义标签进行迭代的示例

在这个例子中,我们将使用自定义标签中的属性,该属性返回任何给定数字的幂。我们在这里创建了三个文件:

  • index.jsp
  • PowerNumber.java
  • mytags.tld

index.jsp

<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>  
  
3 ^ 5 = <m:power number="3" power="5">  
body  
</m:power>  

PowerNumber.java

package cn.javatiku.taghandler;  
  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.TagSupport;  
  
public class PowerNumber extends TagSupport{  
private int number;  
private int power;  
private static int counter;  
private static int result=1;  
  
public void setPower(int power) {  
    this.power = power;  
}  
  
public void setNumber(int number) {  
    this.number = number;  
}  
  
public int doStartTag() throws JspException {  
    return EVAL_BODY_INCLUDE;  
}  
  
public int doAfterBody() {  
    counter++;   
    result *= number;   
    if (counter==power)   
      return SKIP_BODY;   
    else   
      return EVAL_BODY_AGAIN;   
  }   
  
public int doEndTag() throws JspException {  
    JspWriter out=pageContext.getOut();  
    try{  
        out.print(result);  
    }catch(Exception e){e.printStackTrace();}  
      
    return EVAL_PAGE;  
}  
}  

mytags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>  
<!DOCTYPE taglib  
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  
  
<taglib>  
  <tlib-version>1.0</tlib-version>  
  <jsp-version>1.2</jsp-version>  
  <short-name>simple</short-name>  
  <uri>http://tomcat.apache.org/example-taglib</uri>  
  <description>A simple tab library for the examples</description>  
  
  <tag>  
    <name>power</name>  
    <tag-class>cn.javatiku.taghandler.PowerNumber</tag-class>  
      
    <attribute>  
    <name>number</name>  
    <required>true</required>  
    </attribute>  
      
    <attribute>  
    <name>power</name>  
    <required>true</required>  
    </attribute>  
    
  </tag>  
</taglib>  

创建循环标签(为循环创建标签)

我们创建一个循环标签,该标签迭代此标签的体内容。

index.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<title>Insert title here</title>  
</head>  
<body>  
  
<%@taglib prefix="m" uri="sssuri" %>  
<m:loop end="5" start="1">  
<p>My Name is khan</p>  
</m:loop>   
  
</body>  
</html>  

mytags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>  
<!DOCTYPE taglib  
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  
<taglib>  
  <tlib-version>1.0</tlib-version>  
  <jsp-version>1.2</jsp-version>  
  <short-name>abc</short-name>  
    
  <uri>sssuri</uri>  
 <tag>  
    <name>loop</name>  
    <tag-class>cn.javatiku.customtag.Loop</tag-class>  
     
    <attribute>  
    <name>start</name>  
    <required>true</required>  
    </attribute>  
      
    <attribute>  
    <name>end</name>  
    <required>true</required>  
    </attribute>  
 </tag>  
   
</taglib>  

Loop.java

package cn.javatiku.customtag;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.tagext.TagSupport;  
  
public class Loop extends TagSupport{  
    private int start=0;  
    private int end=0;  
      
    public void setStart(int start) {  
        this.start = start;  
    }  
    public void setEnd(int end) {  
        this.end = end;  
    }  
      
    @Override  
    public int doStartTag() throws JspException {  
        return EVAL_BODY_INCLUDE;  
    }  
  
    @Override  
    public int doAfterBody() throws JspException {  
        if(start<end){  
            start++;  
            return EVAL_BODY_AGAIN;  
        }else{  
        return SKIP_BODY;  
       }  
          
    }  
  
      
}  

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  
<jsp-config>  
<taglib>  
<taglib-uri>sssuri</taglib-uri>  
<taglib-location>/WEB-INF/mytags.tld</taglib-location>  
</taglib>  
</jsp-config>  
  
</web-app>  

输出

6-1.jpg

标签: JSP, JSP教程, JSP技术, JSP快速入门, JSP简单用法, JSP编程, JSP下载, JSP基本语法, JSP安装教程, JSP库, JSP指南, JSP基础教程, JSP初级教程, JSP进阶教程