标签的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 进行迭代
divpare 可以看成是初始化
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 divpare() throws JspTagException ...{
// TODO Auto-generated method stub
}
public void setVarStatus(String arg0) ...{
super.setVarStatus(arg0);
}
} |
JSP页面
<%...@ taglib divfix="cc" uri="http://jstlbook.com/tld/weekday.tld" %>
<%...@ taglib divfix="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 |