Struts2 的 append(追加)標籤被用來組合幾個疊代器(以列表或對映建立)成一個單一的疊代器。這裡建立一個Web工程:struts2appendtag,來演示在多個核取方塊如何設定的預設值,整個專案的結構如下圖所示:
在本教學中,將使用Struts2 <s:append>標籤執行以下任務:
AppendTagAction
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 AppendTagAction 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... }
appendIterator.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head><title>Struts2 append 標籤範例 - by www.tw511.com</title> </head> <body> <h1>Struts2 Append 標籤範例</h1> 1. Combine 3 ArrayList into a single iterator. <s:append var="customListIterator"> <s:param value="%{list1}" /> <s:param value="%{list2}" /> <s:param value="%{list3}" /> </s:append> <ol> <s:iterator value="%{#customListIterator}"> <li><s:property /></li> </s:iterator> </ol> 2. Combine 3 HashMap into a single iterator. <s:append var="customMapIterator"> <s:param value="%{map1}" /> <s:param value="%{map2}" /> <s:param value="%{map3}" /> </s:append> <ol> <s:iterator value="%{#customMapIterator}"> <li><s:property /></li> </s:iterator> </ol> 3. Combine ArrayList and HashMap into a single iterator. <s:append var="customMixedIterator"> <s:param value="%{list1}" /> <s:param value="%{map1}" /> </s:append> <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="appendTagAction" class="com.tw511.common.action.AppendTagAction" > <result name="success">/pages/appendIterator.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2appendtag/appendTagAction.action
在瀏覽器中開啟,顯示結果如下:
Struts 2 Append tag example 1. Combine 3 ArrayList into a single iterator. 1. List1 - 1 2. List1 - 2 3. List1 - 3 4. List2 - 1 5. List2 - 2 6. List2 - 3 7. List3 - 1 8. List3 - 2 9. List3 - 3 2. Combine 3 HashMap into a single iterator. 1. map1-key3=map1-value3 2. map1-key1=map1-value1 3. map1-key2=map1-value2 4. map2-key2=map2-value2 5. map2-key3=map2-value3 6. map2-key1=map2-value1 7. map3-key3=map3-value3 8. map3-key1=map3-value1 9. map3-key2=map3-value2 3. Combine ArrayList and HashMap into a single iterator. 1. List1 - 1 2. List1 - 2 3. List1 - 3 4. map1-key3=map1-value3 5. map1-key1=map1-value1 6. map1-key2=map1-value2