logic:iterate
<logic:iterate>标记用于在页面中创建一个循环,以此来遍历如数组、Collection、Map这样的对象。该标记的功能强大,在Struts应用的页面中经常使用到。 <%
String[] testArray={"str1","str2","str3"}; pageContext.setAttribute("test",testArray); %> <logic:iterate id="show" name="test"> <bean:write name="show"/> </logic:iterate> 在上面的代码中,首先定义了一个字符串数组,并为其初始化。接着,将该数组存入pageContext对象中,命名为test1。然后使用<logic:iterate>标记的name属性指定了该数组,并使用id来引用它,同时使用<bean:write>标记来将其显示出来。其结果为: 其中length属性指定了输出元素的个数,offset属性指定了从第几个元素开始输出,如此处为1,则表示从第二个元素开始输出。所以该代码的运行结果应当输出: <logic:iterate id="show" name="test" length="2" offset="1" indexId="number">
<bean:write name="number"/>:<bean:write name="show"/> </logic:iterate> 其显示结果为: <%
HashMap countries=new HashMap(); countries.put("country1","中国"); countries.put("country2","美国"); countries.put("country3","英国"); countries.put("country4","法国"); countries.put("country5","德国"); pageContext.setAttribute("countries",countries); %> <logic:iterate id="country" name="countries"> <bean:write name="country" property="key"/>: <bean:write name="country" property="value"/> </logic:iterate>
<%
String[] colors={"red","green","blue"}; String[] countries1={"中国","美国","法国"}; String[] persons={"乔丹","布什","克林顿"}; ArrayList list2=new ArrayList(); list2.add(colors); list2.add(countries1); list2.add(persons); pageContext.setAttribute("list2",list2); %> <logic:iterate id="first" name="list2" indexId="numberfirst"> <bean:write name="numberfirst"/> <logic:iterate id="second" name="first"> <bean:write name="second"/> </logic:iterate> <br> </logic:iterate>
|