91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Struts2中利用OGNL表達式對map進行遍歷

發布時間:2020-12-07 16:48:00 來源:億速云 閱讀:420 作者:Leah 欄目:編程語言

這篇文章給大家介紹怎么在Struts2中利用OGNL表達式對map進行遍歷,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一.Action中的代碼:MapAction.java

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代碼

<%@ 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)

<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進行遍歷就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

海盐县| 阿拉善右旗| 东辽县| 霍州市| 保山市| 永定县| 江华| 精河县| 巴塘县| 兴化市| 青海省| 长兴县| 衢州市| 泸溪县| 聂拉木县| 绥芬河市| 防城港市| 平泉县| 海盐县| 仙桃市| 科技| 成安县| 融水| 玛纳斯县| 丰镇市| 抚松县| 迁安市| 秦安县| 眉山市| 永修县| 达拉特旗| 吉木萨尔县| 淳安县| 尚义县| 和政县| 得荣县| 盖州市| 田东县| 准格尔旗| 开阳县| 青龙|