zoukankan      html  css  js  c++  java
  • 使用struts1.x标签<logic:iterate>来输出map,list

    logic:iterate标记用于在页面中创建一个循环,以此来遍历如数组,Collection,Map这样的对象。该标记的功能强大,在 Struts应用的页面中经常使用到。


    对数组进行循环遍历
    使用logic:iterate标记可以用于遍历数组。以下是一段示例代码:

    <%
        String [] testArray1 = {"str0","str1","str2","str3","str4","str5","str6"};
        pageContext.setAttribute("test1",testArray1);
    %>
    <logic:iterate id="array1" name="test1">
        <bean:write name="array1"/>
    </logic:iterate>

    在上面的代码中,首先定义了一个字符串数组,并为其初始化。接着,将该数组存入pageContext对象中,命名为test1。然后使用 logic:iterate标记的name属性指定了该数组,并使用id来引用它,同时使用<bean;write>标记来将其显示出来。运行效果如下所示:

    str0
    str1
    str2
    str3
    str4
    str5
    str6

    分析一下上面的代码,可知logic:iterate标记的name属性用于指定某一存在的对象,id属性定义一个新的对象来引用name 属性所指定的对象。再使用<bean:write>标记的name属性来引用了id属性所定义的新的对象,将数组的值打印出来。另外,还可以通过length属性来指定输出元素的个数。如下面的代码。

    <logic:iterate id="array1" name="test1" length="3" offset="2">
        <bean:write name="array1"/>
    </logic:iterate>

    其中length属性指定了输出元素的个数,offset属性指定了从第几个元素开始输出,如此处为2则表示从第三个元素开始输出。所以该代码的运行结果应当从str2一直输出到str4。实际运行效果如下:

    str2
    str3
    str4

    另外,该标记还有一个indexId属性,它指定一个变量存放当前集合中正被访问的元素的序号,如下例:

    <logic:iterate id="array1" name="test1" length="3" offset="2" indexId="number">
    <bean:write name="number"/>:
    <bean:write name="array1"/>
    </logic:iterate>

    运行效果如下所示:

    2: str2
    3: str3
    4: str4

    可以看出,序号已经加在了输出字符串的前面。

    对HashMap进行循环遍历
    以下是一段使用logic:iterate标记遍历HashMap的代码。

    <%
        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>
    上面的代码首先定义了一个HashMap的对象,并向其中添加了5个元素,再把它存入pageContext对象中,命名为countries。令 logic:iterate标记的name属性等于"countries",id为country。再让<bean:write>标记的 name属性和logic:iterate 标记的id属性相对应,令property一个为"key",一个为"value"。便会有以下输出结果:

    country1: 中国
    country4: 法国
    country5: 德国
    country2: 美国
    country3: 英国

    从效果上看出,logic:iterate标记确定遍历了HashMap的每一个元素,但它并未按添加的顺序将其显示出来。这是因为HashMap 是无序存放的。

    对List进行循环遍历
    logic:iterate标记也可以对List型的对象进行遍历,以下是一段示例代码。

    <%
    ArrayList list1 = new ArrayList();
    list1.add("str1");
    list1.add("str2");
    list1.add("str3");
    list1.add("str4");
    list1.add("str5");
    pageContext.setAttribute("testlist",list1);
    %>
    <logic:iterate id="showlist" name="testlist" indexId="index">
    <bean:write name="index"/>:
    <bean:write name="showlist"/>
    </logic:iterate>

    运行效果如下:

    0: str1
    1: str2
    2: str3
    3: str4
    4: str5

    嵌套遍历
    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>

    上面的代码使用了双重循环来遍历一个ArrayList对象,这个ArrayList对象内的每一个元素又是一个String型的数组。以下是运行效果:

    0: red green blue
    1: 中国 美国 法国
    2: 乔丹 布什 克林顿

    转载:http://www.javaeye.com/topic/246277
     
  • 相关阅读:
    Server Develop (三) 多进程实现C/S
    Server Develop (二) 多线程实现C/S
    Server Develop (一) 简单的TCP/IP C/S
    Server Develop (四) select实现非阻塞sever
    js添加、修改、删除xml节点例子
    网站弹出“位于Google Code SubversionRepository 的服务器……”的解决办法
    iframe嵌入网页
    asp空间判断jmail组件是否安装或支持的代码
    IE8的margintop兼容问题
    不错的CMS,值得借鉴!
  • 原文地址:https://www.cnblogs.com/princessd8251/p/3068938.html
Copyright © 2011-2022 走看看