标签的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
(t007)