struts2 遍歷Map的多種方法
?
主要針對以下幾種情況的Map:
?
- ? private ?Map<String,?String>?strMap?=? new ?HashMap<String,?String>();??
- ??
- private ?Map<String,?Person>?perMap?=? new ?HashMap<String,?Person>();??
- ??
- private ?Map<String,?String[]>?strArryMap?=? new ?HashMap<String,?String[]>();??
- ??
- private ?Map<String,?List<Person>>?perLstMap?=? new ?HashMap<String,?List<Person>>();??
下面給出一個示例
?
1).ExpressMapAction.java
?
- import ?java.util.ArrayList;??
- import ?java.util.HashMap;??
- import ?java.util.List;??
- import ?java.util.Map;??
- ??
- import ?com.expre.struts2.bean.Person;??
- import ?com.expre.struts2.bean.Phone;??
- import ?com.opensymphony.xwork2.ActionSupport;??
- ??
- public ? class ?ExpressMapAction? extends ?ActionSupport?{??
- ???? private ? static ? final ? long ?serialVersionUID?=?-4251480679223607716L;??
- ??
- ???? private ?Map<String,?String>?strMap?=? new ?HashMap<String,?String>();??
- ??
- ???? private ?Map<String,?Person>?perMap?=? new ?HashMap<String,?Person>();??
- ??
- ???? private ?Map<String,?String[]>?strArryMap?=? new ?HashMap<String,?String[]>();??
- ??
- ???? private ?Map<String,?List<Person>>?perLstMap?=? new ?HashMap<String,?List<Person>>();??
- ???
- ? //get&set方法,這里都省略了 ??
- ??
- ? @Override ??
- ? public ?String?execute()? throws ?Exception?{??
- ??
- ?? //?值為字符串 ??
- ??strMap.put( "first" ,? "zxx" );??
- ??strMap.put( "second" ,? "lsx" );??
- ??strMap.put( "third" ,? "wxh" );??
- ??
- ?? //?值為javabean對象 ??
- ??Person?person?=? new ?Person( "001" ,? "zxx" ,? 22 );??
- ??person.setPhone( new ?Phone( "apple" ,?18957157189L));??
- ??
- ??perMap.put( "one" ,?person);??
- ??
- ??person?=? new ?Person( "002" ,? "lsx" ,? 25 );??
- ??person.setPhone( new ?Phone( "HTC" ,?18957157187L));??
- ??perMap.put( "two" ,?person);??
- ??
- ?? //?數組處理 ??
- ??strArryMap.put( "arr1" ,? new ?String[]?{? "1" ,? "310000" ,? "hz" ?});??
- ??strArryMap.put( "arr2" ,? new ?String[]?{? "2" ,? "310001" ,? "xh" ?});??
- ??strArryMap.put( "arr3" ,? new ?String[]?{? "3" ,? "310002" ,? "sc" ?});??
- ????
- ?? //list對象處理 ??
- ??List<Person>?list= new ?ArrayList<Person>();????
- ??list.add( new ?Person( "001" ,? "zxx" ,? 22 ));??
- ??list.add( new ?Person( "002" ,? "lsx" ,? 25 ));??
- ??perLstMap.put( "one" ,?list);??
- ????
- ??list= new ?ArrayList<Person>();????
- ??list.add( new ?Person( "003" ,? "wlx" ,? 26 ));??
- ??list.add( new ?Person( "004" ,? "hzx" ,? 28 ));??
- ??perLstMap.put( "two" ,?list);??
- ??
- ?? return ? "result" ;??
- ?}??
- }??
2).strMap.jsp ?
- <html>??
- ?<body>??
- ??<ul>??
- ???<li>訪問Map:<s:property?value= "strMap" /></li>??
- ???<li>訪問Map中某個元素:??
- ????<s:property?value= "strMap.first" />?|??
- ????<s:property?value= "strMap['second']" />?|??
- ????<s:property?value= "strMap[\'third\']" />??
- ???</li>??
- ???<li>訪問Map中所有的key:<s:property?value= "strMap.keys" /></li>??
- ???<li>訪問Map中所有的value:<s:property?value= "strMap.values" /></li>??
- ???<li>訪問容器的大?。??
- ????<s:property?value= "strMap.size()" />?|??
- ????<s:property?value= "strMap.size" /> //這是map特別的地方 ??
- ????</li>??
- ?????
- ????<li>迭代循環取值,最簡單??
- ?????<s:iterator?value= "strMap" ?id= "entry" >??
- ???????<s:property?value= "#entry" />{??
- ????????key:?<s:property?value= "key" />???
- ??????value:<s:property?value= "value" />}|??
- ???????</s:iterator>??
- ??????</li>??
- ???????
- ????<li>迭代循環取值??
- ?????<s:iterator?value= "strMap.keySet()" ?id= "key" >??
- ???????<s:property?value= "key" />或<s:property?value= "#key" />:??
- ???????????<s: if ?test= "strMap[#key]!=null" >???
- ????????????<s:property?value= "strMap.get(#key)" />?|??
- ???????????</s: if >??
- ???????</s:iterator>??
- ??????</li>??
- ???????
- ??????<li>迭代循環取數組值??
- ?????<s:iterator?value= "strMap" ?id= "entry" >??
- ???????<s:property?value= "#entry" />{??
- ???????<s:property?value= "value[0]" />???
- ???????<s:property?value= "value[1]" />??|??
- ???????<s:property?value= "value[2]" />??}??
- ???????</s:iterator>??
- ??????</li>??
- ???????
- ??????<li>迭代循環取對象值,最直接:??
- ???????<ul>??
- ???????<s:iterator?value= "perMap" ?id= "entry" >?????
- ????????<li>??
- ??????????<s:property?value= "#entry" />??{??
- ?????????ID:<s:property?value= "value.ID" />???
- ?????????Name:<s:property?value= "value.name" />???
- ?????????Age:<s:property?value= "value.age" />??}|??
- ?????????</li>??
- ???????</s:iterator>??
- ???????</ul>??
- ??????</li>??
- ???????
- ??????<li>迭代循環取對象的屬性值??
- ????<s:iterator?value= "perMap.keySet()" ?id= "key" >??
- ??????????<s:property?value= "#key" />:??
- ???????????<s:iterator?value= "perMap.get(#key)" >??
- ???????????????<s:property?value= "ID" />??
- ???????????????<s:property?value= "name" />??
- ???????????????<s:property?value= "phone.name" />??
- ???????????</s:iterator>|??
- ??????</s:iterator>??
- ??????</li>??
- ???????
- ??????<li>稍復雜的迭代??
- ???????<s:iterator?value= "perLstMap" ?id= "entry" >????
- ????????<s:set?name= "total" ?value= "#entry.value.size" />????
- ????????<s:iterator?value= "#entry.value" ?status= "s" >????
- ???????????<s: if ?test= "#s.first" >??
- ????????????<s:property?value= "#entry.key" />:共${total}條??
- ???????????</s: if >???{??
- ???????????<s:property?value= "ID" />??|??
- ???????????<s:property?value= "name" />???|??
- ???????????<s:property?value= "age" />??
- ????????????};??
- ????????</s:iterator>????
- ?????</s:iterator>???
- ???</li>??
- ???</ul>??
- ?</body>??
- </html>??
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
