服务器之家

服务器之家 > 正文

Struts2 使用OGNL遍历map方法详解

时间:2021-01-12 14:39     来源/作者:苍蝇也是肉呀

一.Action中的代码:MapAction.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.zx.demo.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.zx.demo.model.Product;
import com.zx.demo.model.Student;
public class MapAction extends ActionSupport
 private Map<String,String> map;
 private Map<String,Student> studentMap;
 private Map<String,String[]> arrayMap;
 private Map<String,List<Student>> listMap; 
 //这里省略get set方法
 public String testMap(){ 
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);
return SUCCESS; 
  
}

 

二.前端测试页面:testMap.jsp

Java代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<%@ page
contentType="text/html;charset=UTF-8"
%>
<%@ taglib prefix="s"
uri="/struts-tags" %>
 <html>
<head>
<title>struts2中的map遍历总结</title>
</head>
<body>
<b>1.map中的key为String,value为String字符串</b><br>
   <s:iterator value="map" id="column">
   <s:property value="#column"/><br>
 key: <s:property value="key"/><br>
 value:<s:property value="value"/><br>
   </s:iterator>
******************************************<br>
 <table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
 <td>key=value</td>
 <td>ID</td>
 <td>num</td>
 <td>name</td>
 <td>sex</td>
 <td>age</td>
</tr>
******************************************<br>
 <b>2.map中的key为String,value为Student对象</b>
<s:iterator value="studentMap" id="column">
<tr>
 <td>
  <s:property value="#column"/></td>
 <td>
  <s:property value="value.id"/></td>
 <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>
</table>
 <p>
 
<table border="1" width="50%" cellspacing="0" cellpadding="0">
 <tr>
   <td>key=value</td>
   <td>ID</td>
   <td>num</td>
   <td>name</td>
   <td>sex</td>
   <td>age</td>
 </tr>
******************************************<br>
<b>3.map中的key为String,value为String数组</b>
<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>
</table>
<p>
<table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
 <td>class</td>
  <td>ID</td>
  <td>num</td>
  <td>name</td>
  <td>sex</td>
  <td>age</td>
</tr>
 ******************************************<br>
<b>4.map中的key为String,value为list集合</b
<s:iterator value="listMap" id="column">
 <s:set name="total" value="#column.value.size"/>
 <s:iterator value="#column.value" status="s">
<tr>
 <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>
 </table>
</body>
</html>

附:map的类型为Map<Integer,List(Menu)>在前端遍历,session.powerList存放的类型是:List(Menu)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<s:iterator value="map" id="mapList">
   <s:if test="#mapList.key==0"><!-key为0的是父级菜单->
   <s:iterator value="#mapList.value" id="map">
    <li class="current"><!--遍历父级菜单,取得其名字-->
   <b><s:property value="#map.menuName" /></b></a>
     <ul>
     <s:iterator value="#session.powerList" id="power">
   <!--遍历父级菜单下的所用子菜单-->
      <s:if test="#power.menuFatherId==#map.menuId">
    <li><a onClick="funFrame('admin/<s:property value="#power.menuUrl" />')" ><s:property value="#power.menuName" /></a>
    </li>
       </s:if>
     </s:iterator>
     </ul>
     </li>
     </s:iterator>
     </s:if>
     </s:iterator>

总结

以上就是本文关于Struts2 使用OGNL遍历map方法详解的全部内容,希望对大家有所帮助。有兴趣的朋友可以参阅:OGNL表达式基本语法与用法详解  Struts2 通过ognl表达式实现投影  Struts2 OGNL表达式实例详解等。感谢大家对本站的支持。

原文链接:http://blog.sina.com.cn/s/blog_7ffb8dd501014k8y.html

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部