struts2 merge标签用来合并几个迭代器(由列表或映射创建)成一个迭代器。这里创建一个web工程:struts2mergetag,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:

mergetagaction
package com.h3.common.action;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import com.opensymphony.xwork2.actionsupport;
public class mergetagaction extends actionsupport{
private list<string> list1 = new arraylist<string>();
private list<string> list2 = new arraylist<string>();
private list<string> list3 = new arraylist<string>();
private map<string,string> map1 = new hashmap<string,string>();
private map<string,string> map2 = new hashmap<string,string>();
private map<string,string> map3 = new hashmap<string,string>();
public string execute() {
list1.add("list1 - 1");
list1.add("list1 - 2");
list1.add("list1 - 3");
list2.add("list2 - 1");
list2.add("list2 - 2");
list2.add("list2 - 3");
list3.add("list3 - 1");
list3.add("list3 - 2");
list3.add("list3 - 3");
map1.put("map1-key1", "map1-value1");
map1.put("map1-key2", "map1-value2");
map1.put("map1-key3", "map1-value3");
map2.put("map2-key1", "map2-value1");
map2.put("map2-key2", "map2-value2");
map2.put("map2-key3", "map2-value3");
map3.put("map3-key1", "map3-value1");
map3.put("map3-key2", "map3-value2");
map3.put("map3-key3", "map3-value3");
return success;
}
//getter methods...
}
jsp页面使用merge标记为3个arraylist/3个hashmap/1个arraylist+1个hashmap合并成一个迭代器,循环它的值,并把打印出来。
merge.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head><title>generator 标签示例 - www.h3.com</title> </head> <body> <h1>struts 2 merge tag example</h1> 1. merge 3 arraylist into a single iterator. <s:merge var="customlistiterator"> <s:param value="%{list1}" /> <s:param value="%{list2}" /> <s:param value="%{list3}" /> </s:merge> <ol> <s:iterator value="%{#customlistiterator}"> <li><s:property /></li> </s:iterator> </ol> 2. merge 3 hashmap into a single iterator. <s:merge var="custommapiterator"> <s:param value="%{map1}" /> <s:param value="%{map2}" /> <s:param value="%{map3}" /> </s:merge> <ol> <s:iterator value="%{#custommapiterator}"> <li><s:property /></li> </s:iterator> </ol> 3. merge arraylist and hashmap into a single iterator. <s:merge var="custommixediterator"> <s:param value="%{list1}" /> <s:param value="%{map1}" /> </s:merge> <ol> <s:iterator value="%{#custommixediterator}"> <li><s:property /></li> </s:iterator> </ol> </body> </html>
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devmode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="mergetagaction" class="com.h3.common.action.mergetagaction" > <result name="success">pages/merge.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2mergetag/mergetagaction.action


在浏览器打开上面的网址,输出以下结果:
struts 2 merge tag example 1. merge 3 arraylist into a single iterator. 1. list1 - 1 2. list2 - 1 3. list3 - 1 4. list1 - 2 5. list2 - 2 6. list3 - 2 7. list1 - 3 8. list2 - 3 9. list3 - 3 2. merge 3 hashmap into a single iterator. 1. map1-key3=map1-value3 2. map2-key2=map2-value2 3. map3-key3=map3-value3 4. map1-key1=map1-value1 5. map2-key3=map2-value3 6. map3-key1=map3-value1 7. map1-key2=map1-value2 8. map2-key1=map2-value1 9. map3-key2=map3-value2 3. merge arraylist and hashmap into a single iterator. 1. list1 - 1 2. map1-key3=map1-value3 3. list1 - 2 4. map1-key1=map1-value1 5. list1 - 3 6. map1-key2=map1-value2