struts2迭代器标签用来迭代一个值,它可以是任何 java.util.collection 或 java.util.iterator的值。在本教程中,您将创建一个列表变量,使用迭代器标签来遍历,并得到使用iteratorstatus迭代状态。
这里创建一个web工程:strut2iterator,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:
action类有列表属性,它包含多种美味 “kfc combo meals”.
iteratorkfcaction
package com.h3.common.action; import java.util.arraylist; import java.util.list; import com.opensymphony.xwork2.actionsupport; public class iteratorkfcaction extends actionsupport{ private list<string> combomeals; public list<string> getcombomeals() { return combomeals; } public void setcombomeals(list<string> combomeals) { this.combomeals = combomeals; } public string execute() { combomeals = new arraylist<string>(); combomeals.add("snack plate"); combomeals.add("dinner plate"); combomeals.add("colonel chicken rice combo"); combomeals.add("colonel burger"); combomeals.add("o.r. fillet burger"); combomeals.add("zinger burger"); return success; } }
下面的jsp页面使用iterator标签来遍历显示所有的“肯德基组合餐”名单。 在迭代器标签,它包含了一个“status”的属性,它用于在iteratorstatus类中声明名称。
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>struts2 <s:iterator>标签示例</h1> <div><div class="ads-in-post hide_if_width_less_800"> <script async class="lazy" data-original="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 728x90 - after2ndh4 --> <ins class="adsbygoogle hide_if_width_less_800" data-ad-client="ca-pub-2836379775501347" data-ad-slot="3642936086" data-ad-region="h3region"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></div><h2>simple iterator</h2> <ol> <s:iterator value="combomeals"> <li><s:property /></li> </s:iterator> </ol> <h2>iterator with iteratorstatus</h2> <table> <s:iterator value="combomeals" status="combomealsstatus"> <tr> <s:if test="#combomealsstatus.even == true"> <td ><s:property/></td> </s:if> <s:elseif test="#combomealsstatus.first == true"> <td><s:property/> (this is first value) </td> </s:elseif> <s:else> <td><s:property/></td> </s:else> </tr> </s:iterator> </table> </body> </html>
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devmode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="iteratorkfcaction" class="com.h3.common.action.iteratorkfcaction" > <result name="success">/pages/iterator.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2iterator/iteratorkfcaction.action