標籤:
該處理器的 父類 javax.servlet.jsp.tagext.SimpleTagSupport
public class ForEach extends SimpleTagSupport {private Collection<?> items;public void setItems(Collection<?> items) {this.items = items;}private String var;public void setVar(String var) {this.var = var;}@Overridepublic void doTag() throws JspException, IOException {//遍曆items集合if(items!=null){for(Object obj:items){//*把正在遍曆的對象放入PageContext中 鍵,var 值,正在遍曆的對象getJspContext().setAttribute(var, obj);//直接輸出到頁面上getJspBody().invoke(null);}}}}
jsp頁面裡面類比資料
<%List<TestTag> tg = new ArrayList<TestTag>();tg.add(new TestTag(1, "AAA"));tg.add(new TestTag(2, "BBB"));tg.add(new TestTag(3, "CCC"));tg.add(new TestTag(4, "DDD"));request.setAttribute("tg", tg);%><!-- 使用標籤庫foreach --><c:forEach items="${requestScope.tg }" var="abc">${abc.id}--${abc.name}<br></c:forEach><!-- 使用自訂標籤foreach --><wlc:ForEachTag items="${requestScope.tg }" var="abc">${abc.id}--${abc.name}<br></wlc:ForEachTag>
tld檔案聲明
<tag><name>ForEachTag</name><tag-class>cn.stud.wlc.tag.ForEach</tag-class><body-content>scriptless</body-content><attribute><name>items</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>var</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag>
標籤處理器類(自訂標籤)ForEach