Struts2 merge標籤用來合併幾個疊代器(由列表或對映建立)成一個疊代器。這裡建立一個Web工程:struts2mergetag,來演示在多個核取方塊如何設定的預設值,整個專案的結構如下圖所示:
MergeTagAction
package com.tw511.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.tw511.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.tw511.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