struts2标签 遍历map集合
首先我们来构造几个map集合。
private Long id;
private String num;
private String name;
private String sex;
private Integer age;
Action中的代码
private Map<String,String> map;
private Map<String,Student> studentMap;
private Map<String,String[]> arrayMap;
private Map<String,List<Student>> listMap; // 实现 四个map对象的get 和set方法。 map=new HashMap<String,String>();
map.put("1", "one");
map.put("2", "two"); studentMap=new HashMap<String,Student>();
studentMap.put("student1",new Student(new Long(1),"20034140201","张三1","男",25));
studentMap.put("student2",new Student(new Long(2),"20034140202","张三2","女",26));
studentMap.put("student3",new Student(new Long(3),"20034140202","张三3","男",27)); arrayMap=new HashMap<String,String[]>();
arrayMap.put("arr1", new String[]{"1","2003401","leejie","male","20"});
arrayMap.put("arr2", new String[]{"2","2003402","huanglie","male","25"});
arrayMap.put("arr3", new String[]{"3","2003403","lixiaoning","male","21"}); listMap=new HashMap<String,List<Student>>(); List<Student> list1=new ArrayList<Student>();
list1.add(new Student(new Long(1),"20034140201","张三1","男",25));
list1.add(new Student(new Long(2),"20034140202","张三2","男",25));
list1.add(new Student(new Long(3),"20034140203","张三3","男",25));
listMap.put("class1", list1); List<Student> list2=new ArrayList<Student>();
list2.add(new Student(new Long(1),"20034140301","李四1","男",20));
list2.add(new Student(new Long(2),"20034140302","李四2","男",21));
list2.add(new Student(new Long(3),"20034140303","李四3","男",22));
list2.add(new Student(new Long(4),"20034140304","李四4","男",23));
listMap.put("class2", list2);
(2).通过上述java代码我们已经构建好了4个map集合。 接下来的重头戏就是如何通过strut2的标签来获取map集合中的值。
1.map中的value为String字符串
<s:iterator value="map" id="column">
<s:property value="#column"/><br> //这里获取到的值为key=value 即:键值对
key: <s:property value="key"/><br> //这里的key为内置的,我们只要在value中写上key 即会有值
value:<s:property value="value"/><br> //同样这里的value也为内置的
</s:iterator> <b>2.map中的value为Student对象</b>
<s:iterator value="studentMap" id="column">
<tr>
<td><s:property value="#column"/></td>
<td><s:property value="key"/></td>
<td><s:property value="value"/></td> //这里的value返回的是一个student对象
<td><s:property value="value.id"/></td> //这里获取student对象中的属性值
<td><s:property value="value.num"/></td>
<td><s:property value="value.name"/></td>
<td><s:property value="value.sex"/></td>
<td><s:property value="value.age"/></td>
</tr>
</s:iterator>
遍历studentMap 还可以用下面方式,跟上面方式效果是一样的
2.map中的value为Student对象
<s:iterator value="studentMap" id="column">
<tr>
<td><s:property value="#column"/></td>
<td><s:property value="key"/></td>
<s:iterator value="value">
<td><s:property value="id"/></td>
<td><s:property value="num"/></td>
<td><s:property value="name"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="age"/></td>
</s:iterator>
</tr>
</s:iterator>
3.map中的value为String数组
<s:iterator value="arrayMap" id="column">
<tr>
<td><s:property value="#column"/></td> <!--同时取出键和值-->
<td><s:property value="value[0]"/></td>
<td><s:property value="value[1]"/></td>
<td><s:property value="value[2]"/></td>
<td><s:property value="value[3]"/></td>
<td><s:property value="value[4]"/></td>
</tr>
</s:iterator>
4.map中的value为list集合
<s:iterator value="listMap" id="column">
<s:set name="total" value="#column.value.size"/> //注意<s:set 标签的用法
<s:iterator value="#column.value" status="s"> //这里#column.value 还是一个student的list集合,因而需要再次迭代一次
<tr>
<s:property value="#s.first"/> //判断是不是集合中的第一个对象
<s:if test="#s.first">
<td rowspan="${total}">
<s:property value="#column.key"/>
</td>
</s:if>
<td><s:property value="id"/></td>
<td><s:property value="num"/></td>
<td><s:property value="name"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</s:iterator>
struts2标签 遍历map集合的更多相关文章
- 遍历Map集合:java.util.Map.Entry、KeySet两种方式
遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合
package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...
- (1)集合 ---遍历map集合
Map接口 实现Map接口的类用来存储键(key)-值(value) 对.Map 接口的实现类有HashMap和TreeMap等.Map类中存储的键-值对通过键来标识,所以键值不能重复. Ha ...
- 遍历Map集合的几种方式
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entr ...
- JSP的C标签遍历Map数据
JSP的C标签遍历Map数据 Map可以实现较为丰富的数据封装. 第一种: 控制器传递到页面的map格式如下: Map<String, User> dataMap = new HashMa ...
- 用来遍历map集合的方法
map集合是以键值对进行存储值的,所以遍历map集合无非就是获取键和值,根据实际需求,进行获取键和值. 1.无非就是通过map.keySet()获取到值,然后根据键获取到值. for(String s ...
- Java之五种遍历Map集合的方式
摘要:在java中所有的map都实现了Map接口,因此所有的Map都可以用以下的方式去遍历. 在java中所有的map都实现了Map接口,因此所有的Map都可以用以下的方式去遍历.这篇文章主要给大家介 ...
- 使用Struts2标签遍历集合
遍历Map<String,Object> 遍历Map<Stirng,List<Student>> 遍历List<Map<String,Student&g ...
- MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法 和sql片段)(三)
一.字段名与属性名(数据库的名字)不一样怎么办? 方案一:在小配置中配置一个resultMapper <!--方案一:resultMapper 字段名与属性名不一致 --> <res ...
随机推荐
- Remark of BLENDFUNCTION from MSDN
Remarks When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, ...
- ubuntu导出文件
ye@aliyun:python$ ./deploy.sh backup static-rw-r--r-- 1 ye ye 174K 2014-03-22 10:36 ./backup/fbz_sta ...
- .NET笔试题(关于迭代的:遍历XML中的FileName)
一.使用迭代算法,遍历XML文件中所有的文件名 写一个函数返回文件名集合 在主函数中调用该方法: 以后有关迭代的都写到这儿
- jQuery实现iframe的自适应高度
假设我们在当前页面要嵌套一个iframe 1 <iframe id="myframe" src="test.html" height="240& ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
- 【简译】Windows 线程基础
翻译一篇关于windows线程的文章,原文在此.第一次翻译,如有错误请多指教 =========================================华丽的分割线============== ...
- vs2010自带的报表
本例用来显示Northwind中的order details表中的数据交分组 1.建立一WinForm程序,并建立一数据库连接,选择order details表,此时会自动建立一个xsd的数据集类,如 ...
- [置顶] linux内核启动1-启动参数(启动参数的获取和处理,分析setup_arch)
最近公司要求调试一个内核,启动时有问题,所以就花了一点时间看看内核启动. 看的过程中总结了一点东西,希望可以帮助大家调试内核. 当我开始看的时候,第一件事是从网上搜集资料,不看不知道,一看吓一跳!牛人 ...
- CF -- 414A
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int mai ...
- Excel通过宏创建百万数据
打开视图->宏->编辑,代码如下,cells(n,m)表示当前Excel表格第n行第m列</span> Sub newdata() Dim i As Long Cells(i, ...