您的位置:首页 > Web前端 > JavaScript

使用JSTL开发jsp自定义标签开发---迭代标签

2007-05-17 12:46 776 查看
标签的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/dtd/web-jsptaglibrary_1_2.dtd">


<taglib>


<tlib-version>1.0</tlib-version>


<jsp-version>1.2</jsp-version>


<short-name>book</short-name>


<uri>http://jstlbook.com/tld/weekday.tld</uri>


<tag>


<name>if</name>


<tag-class>ttt.ConditionTest</tag-class>


<attribute>


<name>items</name>


</attribute>


<attribute>


<name>var</name>


</attribute>


<attribute>


<name>varStatus</name>


</attribute>


</tag>


</taglib>



标签类:

几个地方需要说明

items属性是jsp页面传来的需要迭代的collection

hasNext方法返回collection是否迭代结束

next 进行迭代

prepare 可以看成是初始化

setVarStatus方法可以监视循环状态,根c:forEach的类似


package ttt;


import javax.servlet.jsp.JspTagException;


import javax.servlet.jsp.jstl.core.LoopTagSupport;








public class LoopTagTest extends LoopTagSupport ...{


private String items;


private int i=0;




public String getItems() ...{


return items;


}






public void setItems(String items) ...{


this.items = items;


}






protected boolean hasNext() throws JspTagException ...{


if(i==0)




...{


return true;




}else...{


return false;


}


}






protected Object next() throws JspTagException ...{


i=1;


return this.getItems();


}






protected void prepare() throws JspTagException ...{


// TODO Auto-generated method stub





}






public void setVarStatus(String arg0) ...{


super.setVarStatus(arg0);


}










}





JSP








<%...@ taglib prefix="cc" uri="http://jstlbook.com/tld/weekday.tld" %>




<%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




<html>


<head>




<title>Currency Formatting</title>


</head>


<body>


<cc:if items="a:b:c:d" var="line" varStatus="s">


<c:out value="${s.first}"/><br>


<c:forTokens items="${line}" delims=":" var="field">


<c:out value="${field}"/><br>


</c:forTokens>


</cc:if>


</body>


</html>





结果很容易,因为只有一个String,所以外层循环只循环一次,内层根据delims循环四次



true
a
b
c
d
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: