merge標(biāo)簽合并標(biāo)記需要兩個(gè)或兩個(gè)以上的列表作為參數(shù),并把它們合并在一起,如下所示:
1
2
3
4
5
6
7
8
|
<s:merge var= "myMergedIterator" > <s:param value= "%{myList1}" /> <s:param value= "%{myList2}" /> <s:param value= "%{myList3}" /> </s:merge> <s:iterator value= "%{#myMergedIterator}" > <s:property /> </s:iterator> |
如果有兩個(gè)列表A和B的值,A1,A2和B1,B2。合并列表,會(huì)給出A1,B1,A2,B2。
創(chuàng)建動(dòng)作類:
首先,讓我們創(chuàng)建一個(gè)簡(jiǎn)單的類叫做Employee.java,它看起來(lái)像:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package com.yiibai.struts2; import java.util.ArrayList; import java.util.List; import org.apache.struts2.util.SubsetIteratorFilter.Decider; public class Employee { private String name; private String department; public Employee(){} public Employee(String name,String department) { this .name = name; this .department = department; } private List employees; private List contractors; public String execute() { employees = new ArrayList(); employees.add( new Employee( "George" , "Recruitment" )); employees.add( new Employee( "Danielle" , "Accounts" )); employees.add( new Employee( "Melissa" , "Recruitment" )); employees.add( new Employee( "Rose" , "Accounts" )); contractors = new ArrayList(); contractors.add( new Employee( "Mindy" , "Database" )); contractors.add( new Employee( "Vanessa" , "Network" )); return "success" ; } public Decider getRecruitmentDecider() { return new Decider() { public boolean decide(Object element) throws Exception { Employee employee = (Employee)element; return employee.getDepartment().equals( "Recruitment" ); } }; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this .department = department; } public List getEmployees() { return employees; } public void setEmployees(List employees) { this .employees = employees; } public List getContractors() { return contractors; } public void setContractors(List contractors) { this .contractors = contractors; } } |
Employee類有兩個(gè)屬性 - name 和 department,我們也有兩個(gè)員工名單 - employees 和contractors。我們有一個(gè)方法叫做getRecruitmentDecider,返回Decider 對(duì)象。Decider 實(shí)現(xiàn)返回true,如果雇員招聘部門工作,否則返回false。
接下來(lái),讓我們創(chuàng)建一個(gè)DepartmentComparator 比較Employee對(duì)象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.yiibai.struts2; import java.util.Comparator; public class DepartmentComparator implements Comparator { public int compare(Employee e1, Employee e2) { return e1.getDepartment().compareTo(e2.getDepartment()); } @Override public int compare(Object arg0, Object arg1) { return 0 ; } } |
在上面的例子所示,部門比較的基礎(chǔ)上按字母順序排列的部門員工進(jìn)行比較。
創(chuàng)建視圖
創(chuàng)建一個(gè)文件叫做employee.jsp以下內(nèi)容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page contentType= "text/html; charset=UTF-8" %> <%@ taglib prefix= "s" uri= "/struts-tags" %> <html> <head> <title>Employees</title> </head> <body> <b>Employees and Contractors Merged together</b> <br /> <s:merge id= "allemployees" > <s:param value= "employees" /> <s:param value= "contractors" /> </s:merge> <s:iterator value= "allemployees" > <s:property value= "name" />, <s:property value= "department" /><br/> </s:iterator> </body> </html> |
合并標(biāo)記需要兩個(gè)或兩個(gè)以上的列表作為參數(shù)。我們需要給合并一個(gè)id,這樣我們就可以重用它。在這個(gè)例子中,我們提供了作為參數(shù)傳遞給員工和承包商的合并標(biāo)簽。然后,我們使用“allemployees”ID合并列表遍歷并打印員工的細(xì)節(jié)。
配置文件
struts.xml中應(yīng)該像這樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<? 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 = "helloworld" extends = "struts-default" > < action name = "employee" class = "com.yiibai.struts2.Employee" method = "execute" > < result name = "success" >/employee.jsp</ result > </ action > </ package > </ struts > |
web.xml中,應(yīng)該像這樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0" > < display-name >Struts 2</ display-name > < welcome-file-list > < welcome-file >index.jsp</ welcome-file > </ welcome-file-list > < filter > < filter-name >struts2</ filter-name > < filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class > </ filter > < filter-mapping > < filter-name >struts2</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app > |
右鍵點(diǎn)擊項(xiàng)目名稱,并單擊 Export > WAR File創(chuàng)建一個(gè)WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動(dòng)Tomcat服務(wù)器和嘗試訪問(wèn)URL http://localhost:8080/HelloWorldStruts2/employee.action。這會(huì)給出以下畫面: