Struts2 <s:merge>標簽示例
Struts2 merge標簽用來合並幾個迭代器(由列表或映射創建)成一個迭代器。這裡創建一個Web工程:struts2mergetag,來演示在多個複選框如何設置的默認值,整個項目的結構如下圖所示:
在本教學中,將使用Struts2 <s:merge>標簽執行以下任務:
-
合並三個ArrayList到一個迭代器
-
合並三個HashMap到一個迭代器
-
合並ArrayList和HashMap到一個迭代器
假設有2個迭代器,每個有兩個條目,使用merge標記合並成一個迭代後,這些條目的順序將類似於以下內容:
-
第一個迭代的第一個條目。
-
第二個迭代器的第一項
-
第一個迭代器的第二項
-
第二個迭代器的第二項。
這僅適用於列表迭代器;映射迭代器,順序將是隨機的。
1. 動作
Action類有3個ArrayList 和 3 個 HashMap屬性。
MergeTagAction
package com.gitbook.netmon.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... }
2. Merge 標簽示例
JSP頁麵使用merge標記為3個ArrayList/3個HashMap/1個ArrayList+1個HashMap合並成一個迭代器,循環它的值,並把打印出來。
merge.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head><title>Generator 標簽示例 - www.gitbook.net</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>
3. struts.xml
<?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.gitbook.netmon.action.MergeTagAction" > <result name="success">pages/merge.jsp</result> </action> </package> </struts>
4. 示例
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
參考