来源
http://hi.baidu.com/iduany/item/19f9f622603ba8172a0f1c72
struts2 遍历Map的N中方法
主要针对以下几种情况的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
package com.expre.struts2.action;
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).struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="express" extends="struts-default" namespace="/exp">
<action name="expressMap" class="com.expre.struts2.action.ExpressMapAction">
<result name="result">/jsp/strMap.jsp</result>
</action>
</package>
</struts>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/first.jsp</welcome-file>
</welcome-file-list>
</web-app>
4).strMap.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/struts-tags" prefix="s"%>
<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>
5).通过url:http://localhost:8080/struts2Jsp/exp/expressMap.action访问就会看到输出!
别的不多说,strMap.jsp已经把使用方法都写出来了!