首先创建一个User对象
User user=new User();

user.setUserName("张三");

user.setAge(30);

User user1=new User();

user1.setUserName("李四");

user1.setAge(32);


一、迭代List数据

List list=new ArrayList();


1、list中放User对象

list.add(user);list.add(user1);//list中有两条数据

jsp页面:

 <s:iterator id="li" value="list">

 
姓名:<s:property value="#list.userName"/><br />

 
年龄:<s:property value="#list.age"/>

 </s:iterator>


2、list中放String字符串

 list.add("百度");

 list.add("http://www.baidu.com");

jsp页面:


<s:iterator value="list" status="s" id="li">


List中的数据:<s:property />


是否是奇数行,是返回true,否则返回false:<s:property value="#s.odd"/>


是否是偶数行:<s:property value="#s.even"/>


当前行数(返回当前迭代了几个元素):<s:property value="#s.count"/>


list的下标:<s:property value="#s.index"/>


是否第一行:<s:property value="#s.first"/>


是否最后一行:<s:property value="#s.last"/>


当前行数取模:<s:property value="#s.modules(int)"/>


</s:iterator>


由于iterator  status对象并不是ognl的根对象因此访问需要加上 #访问


二、迭代String数组

String[] str={"1","2","3"};

jsp页面:


<s:iterator value="str" status="s">  


<s:iterator value="str[#s.index]">  <!--#s.index:获取下标 -->

<s:property value="str[#s.index][0]"/> <!--直接获取为0的数据,也就是数组中的第一个元素 -->


<s:property />  <!--遍历数组每一个元素-->


</s:iterator>  
   


</s:iterator> 


三、迭代Map

Map textMap=new HashMap();


1、map中放String字符串

textMap.put("1","happy");

textMap.put("2","metohappy");

jsp页面:


<s:iterator value="textMap" id="textMap"> 


key:${textMap.key}


value:${textMap.value}


还可以写成:


key: <s:property value="key"/><br> 

     value:<s:property value="value"/><br> 


</s:iterator>


2、map中放User对象

textMap.put("1",user);

textMap.put("2",user1);

jsp页面:

 <s:iterator value="textMap" id="textMap">  

     姓名:<s:property value="value.userName"/> <br />

     年龄:<s:property value="value.age"/><br />

  </s:iterator> 


3、map中放String数组


String[] baidu={"baidu","http://www.baidu.com"};


String[] sogou={"sogou","http://www.sogou.com"};


textMap.put("1",baidu);


textMap.put("2",sogou);

jsp页面:


<s:iterator value="textMap" id="textMap"> 

     <s:property value="value[0]"/> 

     <s:property value="value[1]"/> 

   </s:iterator> 


4、map中放List

List list=new ArrayList();

List list1=new ArrayList();

①、list中放User对象


list.add(user);list.add(user1);//list中有两条数据


list1.add(user);list1.add(user1);//list1中有两条数据


textMap.put("1",list);


textMap.put("2",list1);


   jsp页面:


   <s:iterator value="textMap" id="textMap"> 


    <s:set name="total" value="#textMap.value.size"/> 


    <s:iterator value="#textMap.value" status="s"> 


       姓名:<s:property value="userName"/>


       年龄:<s:property value="age"/>


    </s:iterator> 

      </s:iterator>

②、List中放String字符串


list.add("百度");

 
list.add("http://www.baidu.com");

 
list1.add("搜狗");

 
list1.add("http://www.sogou.com");

 
textMap.put("1",list);


textMap.put("2",list1);


jsp页面:


<s:iterator value="textMap" id="textMap">    


<s:set name="total" value="#textMap.value.size"/>    


<s:iterator value="#textMap.value" status="s" id="li"> 


textMap的键:${textMap.key} 


list中的值:<s:property />           


</s:iterator>
       


</s:iterator> 


 

原文:http://www.cnblogs.com/yezhenhan/archive/2011/12/21/2296069.html

      http://blog.csdn.net/mr_walter/article/details/7930323

jsp struts标签迭代各种数据的更多相关文章

  1. struts2学习(8)struts标签1(数据标签、控制标签)

    一.struts2标签简介: struts标签很多,功能强大,这是优点: 但是缺点的话,性能方面可能会,各方面速度啊啥的会降低:有人比较测试,struts性能比jstl低很多:   二.struts2 ...

  2. JSP的C标签遍历Map数据

    JSP的C标签遍历Map数据 Map可以实现较为丰富的数据封装. 第一种: 控制器传递到页面的map格式如下: Map<String, User> dataMap = new HashMa ...

  3. <s:iterator>标签迭代数据不显示

    <s:iterator>标签迭代数据不显示 <s:iterator value="#request.voteOptionList" var="voteO ...

  4. Struts标签、Ognl表达式、el表达式、jstl标签库这四者之间的关系和各自作用

    我之前虽然会用,但是一直分不清彼此之间有什么区别,所以查找资料,将它们进行整合区分,加深了解, 一 介绍 1.Struts2的作用   Struts2标签库提供了主题.模板支持,极大地简化了视图页面的 ...

  5. struts标签与jstl标签互换

    近期在做struts切换spring mvc时发现代码中使用了大量的struts标签,对常用的struts标签做了总结,首先需要引入 <%@ taglib prefix="c" ...

  6. Struts标签库详解【2】

    ==================================================================== 需要在head中用<s:head />标签 一.控 ...

  7. Spring MVC -- JSP标准标签库(JSTL)

    JSP标准标签库(JavaServer Pages Standard Tag Library,JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能.JSTL支持通用的.结构化的任务,比如迭 ...

  8. ongl与Struts标签

    一.ONGL OGNL 的全称是“Object-Graph Navigation Language”,即对象图导航语言,它是一种功能强大的开源表达式语言.使用这种表达式语言可以通过某种表达式语法存取  ...

  9. JSP 标准标签库(JSTL)

    JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签, ...

随机推荐

  1. [RVM is not a function] Interating RVM with gnome-terminal

    Ubuntu 12.04 64bit LTS, running the 'rvm use 1.9.3' brings the 'RVM is not a function' warning. Here ...

  2. android apk简单反编译

    1. 查看或者提取资源文件:     使用谷歌官方工具apktool,命令行如下: apktool d xxx.apk xxx_decode d代表反编译,xxx.apk为你要反编译的apk,xxx_ ...

  3. 让DIV中的内容水平和垂直居中

    让一个层水平垂直居中是一个非常常见的布局方式,但在html中水平居中使用margin:0px auto;可以实现,但垂直居中使用外边距是无法达到效果的.(页面设置height:100%;是无效的),这 ...

  4. Hibernate中in语句ids

    Query query=session.createQuery("from User where id in (:ids)"); query.setParameterList(&q ...

  5. C语言:其他进制数转换为十进制(方法一)

    #include<stdio.h> #include<math.h> #include<string.h> //其他任意进制转换为十进制 int main() { ...

  6. lodash的运用

    Object 对象 1._.merge(object, [sources]) 此方法类似于_.assign,除了它递归地将源对象的自有和继承的可枚举字符串键控属性合并到目标对象中. 如果存在目标值,将 ...

  7. 在C#中使用json字符串

    http://jingyan.baidu.com/article/6fb756ecd2b051241858fbef.html

  8. shell各种执行方式区别

    shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别 原文出处:http://blog.csdn.net/dance_rise/article/deta ...

  9. OSGMFC

    在OSG的Demo中找到MFC_OSG类文件. #pragma once #include <osgViewer/Viewer> #include <osgViewer/Viewer ...

  10. [GeoServer]重拾GeoServer之安装篇

    GeoServer的项目是一个完整的Java(J2EE)系统,现实了OpenGIS联盟的网络功能服务器规范和网络覆盖服务器规范,并且集成了Web地图服务器. 在大三的时候WebGIS课程中老师讲解过一 ...