[struts2]struts标签遍历map[转]

时间:2021-12-22 11:04:04

转至:http://hanxin0311.iteye.com/blog/1745792

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>>();

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";
}
}

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