logic:iterate (1)

  1. <logic:iterate>
  2. <logic:iterate>标记用于在页面中创建一个循环,以此来遍历如数组,Collection,Map这样的对象。该标记的功能强大,在 Struts应用的页面中经常使用到。
  3. 对数组进行循环遍历
  4. 使用<logic:iterate>标记可以用于遍历数组。以下是一段示例代码:
  5. <%
  6.     String [] testArray1 = {"str0","str1","str2","str3","str4","str5","str6"};
  7.     pageContext.setAttribute("test1",testArray1);
  8. %>
  9. <logic:iterate id="array1" name="test1">
  10.     <bean:write name="array1"/>
  11. </logic:iterate>
  12. 在上面的代码中,首先定义了一个字符串数组,并为其初始化。接着,将该数组存入pageContext对象中,命名为test1。然后使用 <logic:iterate>标记的name属性指定了该数组,并使用id来引用它,同时使用<bean;write>标记来将其显示出来。运行效果如下所示:
  13. 分析一下上面的代码,可知<logic:iterate>标记的name属性用于指定某一存在的对象,id属性定义一个新的对象来引用name 属性所指定的对象。再使用<bean:write>标记的name属性来引用了id属性所定义的新的对象,将数组的值打印出来。另外,还可以通过length属性来指定输出元素的个数。如下面的代码。
  14. <logic:iterate id="array1" name="test1" length="3" offset="2">
  15.     <bean:write name="array1"/>
  16. </logic:iterate>
  17. 其中length属性指定了输出元素的个数,offset属性指定了从第几个元素开始输出,如此处为2则表示从第三个元素开始输出。所以该代码的运行结果应当从str2一直输出到str4。实际运行效果如下:
  18. 另外,该标记还有一个indexId属性,它指定一个变量存放当前集合中正被访问的元素的序号,如下例:
  19. <logic:iterate id="array1" name="test1" length="3" offset="2" indexId="number">
  20. <bean:write name="number"/>:
  21. <bean:write name="array1"/>
  22. </logic:iterate>
  23. 运行效果如下所示:
  24. 可以看出,序号已经加在了输出字符串的前面。
  25. 对HashMap进行循环遍历
  26. 以下是一段使用<logic:iterate>标记遍历HashMap的代码。
  27. <%
  28.     HashMap countries = new HashMap();
  29.     countries.put("country1","中国");
  30.     countries.put("country2","美国");
  31.     countries.put("country3","英国");
  32.     countries.put("country4","法国");
  33.     countries.put("country5","德国");
  34.     pageContext.setAttribute("countries",countries);
  35. %>
  36. <logic:iterate id="country" name="countries">
  37. <bean:write name="country" property="key"/>: <bean:write name="country" property="value"/>
  38. </logic:iterate> 
  39. 上面的代码首先定义了一个HashMap的对象,并向其中添加了5个元素,再把它存入pageContext对象中,命名为countries。令<logic:iterate>标记的name属性等于"countries",id为country。再让<bean:write>标记的name属性和<logic:iterate> 标记的id属性相对应,令property一个为"key",一个为"value"。便会有以下输出结果:
  40. 从效果上看出,<logic:iterate>标记确定遍历了HashMap的每一个元素,但它并未按添加的顺序将其显示出来。这是因为HashMap 是无序存放的。
  41. 对List进行循环遍历
  42. <logic:iterate>标记也可以对List型的对象进行遍历,以下是一段示例代码。
  43. <%
  44. ArrayList list1 = new ArrayList();
  45. list1.add("str1");
  46. list1.add("str2");
  47. list1.add("str3");
  48. list1.add("str4");
  49. list1.add("str5");
  50. pageContext.setAttribute("testlist",list1);
  51. %>
  52. <logic:iterate id="showlist" name="testlist" indexId="index">
  53. <bean:write name="index"/>:
  54. <bean:write name="showlist"/>
  55. </logic:iterate>
  56. 运行效果如下:
  57. 嵌套遍历
  58. <logic:iterate>标记还可以进行嵌套遍历,以下是一段代码示例:
  59. <%
  60. String [] colors = {"red","green","blue"};
  61. String [] countries1 = {"中国","美国","法国"};
  62. String [] persons = {"乔丹","布什","克林顿"};
  63. ArrayList list2 = new ArrayList();
  64. list2.add(colors);
  65. list2.add(countries1);
  66. list2.add(persons);
  67. pageContext.setAttribute("list2",list2);
  68. %>
  69. <logic:iterate id="first" name="list2" indexId="numberfirst">
  70.      <bean:write name="numberfirst"/>:
  71.      <logic:iterate id="second" name="first">
  72.          <bean:write name="second"/>
  73.      </logic:iterate>
  74.      <br>
  75. </logic:iterate>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值