Struts2教學
Struts2快速入門
Struts2 MVC架構
Struts2 是什麼?
Struts2 環境配置
Struts2 體系結構
Struts2 Hello World 實例
Struts2 配置文件
Struts2 Action/動作
Struts2 攔截器
Struts2 結果和結果類型
Struts2 值棧/OGNL
Struts2 文件上傳
Struts2 MySQL數據庫訪問
Struts2 發送郵件Email
Struts2 驗證框架
Struts2 基於XML校驗
Struts2 本地化/國際化(i18n)
Struts2 類型轉換
Struts2 主題和模板
Struts2 異常處理
Struts2 註釋
Struts2 註釋類型
Struts2 控件標籤
Struts2 iterator標籤
Struts2 if/else標籤
Struts2 merge標籤
Struts2 append標籤
Struts2 generator標籤
Struts2 數據標籤
Struts2 action標籤
Struts2 include標籤
Struts2 bean 標籤
Struts2 date 標籤
Struts2 param 標籤
Struts2 property 標籤
Struts2 set 標籤
Struts2 text 標籤
Struts2 url標籤
Struts2 表單標籤
Struts2 Ajax 標籤
Struts2和Spring集成
Struts2和Tiles集成
Struts2和Hibernate集成
Struts 2 hello world (XML版本)
Struts2註解示例
Struts2 @ResultPath註釋示例
Struts2 include(包含)多個配置文件
Struts2命名空間配置和解釋
Struts2開發者模式
如何刪除Struts2動作的後綴擴展名
使用Struts2動作
Struts2的ActionError&ActionMessage示例
Struts2模型驅動實例
Struts2映射攔截動作
Struts2重寫攔截器參數
Struts2攔截器棧的例子
Struts2 execAndWait攔截器例子
Struts2 <s:textfield>文本示例
Struts2 <s:password>密碼示例
Struts2 <s:hidden>隱藏值例子
Struts2 <s:textarea>文本域實例
Struts2 <s:radio>單選按鈕例子
Struts2單選預選按鈕值
Struts2 <s:checkbox>複選框例子
Struts2設置多個複選框缺省值
Struts2 <s:checkboxlist>多個複選框例子
Struts2 <s:select>下拉框例子
Struts2自動選擇下拉框的值
Struts2 <s:combobox>組合框的例子
Struts2 <s:head>示例
Struts2文件上傳例子
Struts2上傳多個文件例子
Struts2 <s:doubleselect>示例
Struts2 <s:updownselect>示例
Struts2 <s:optiontransferselect>示例
Struts2 <sx:datetimepicker>示例
Struts2 <sx:autocompleter>自動提示例子
Struts2 autocompleter+JSON例子
Struts2 <s:iterator>迭代器標籤示例
Struts2 if,elseif,else標籤示例
struts2 <s:append>標籤示例
Struts2 <s:generator>標籤示例
Struts2 <s:merge>標籤示例
Struts2 <s:sort>標籤示例
Struts2 <s:subset>標籤示例
Struts2 <s:a>標籤示例
Struts2 <s:action>標籤示例
Struts2 <s:bean>標籤示例
Struts2 <s:date>標籤示例
Struts2 <s:debug>標籤示例
Struts2 <s:include>標籤示例
Struts2 <s:i18n>標籤示例
Struts2 <s:param>標籤示例
Struts2 <s:property>標籤實例
Struts2 <s:push>標籤示例
Struts2 <s:set>標籤示例
Struts2 <s:texttag>標籤示例
Struts2 <s:url>標籤示例
Struts2資源包使用示例
Struts2本地化和國際化
Struts2 key鍵屬性示例
Struts2中文本地化問題
如何配置Struts2全局資源包
Struts2+Spring集成實例
Struts2+Quartz2調度集成實例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2+Hibernate集成實例
Struts2+Spring+Hibernate集成實例
Struts2+Log4j集成
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什麼不同?
Struts2獲取HttpServletRequest實例
Struts2獲取HttpServletResponse實例
如何獲取ServletContext對象
Struts2配置Action類的靜態參數
Struts2下載文件實例
Struts2和JSON實例

Struts2 append標籤

說,如果有兩個列表A和B的值A1,A2和B1,B2。合併列表,會給你的A1,A2,B1,B2,而append 名單,會有A1,A2,B1,B2。

創建動作類:

首先,讓我們創建一個簡單的類叫做Employee.java,它看起來像:

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類有兩個屬性 - name 和 department,我們也有兩個員工名單 - employees 和contractors。我們有一個方法叫做getRecruitmentDecider,返回Decider 對象。Decider 實現返回true,如果僱員招聘部門工作,否則返回false。

接下來,讓我們創建一個DepartmentComparator比較Employee對象:

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; } }

在上面的例子所示,部門比較的基礎上按字母順序排列的部門員工進行比較。

創建視圖

創建一個文件叫做employee.jsp以下內容:

<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> Employees Employees and Contractors Merged together
<s:append id="allemployees"> <s:param value="employees" /> <s:param value="contractors" /> </s:append > <s:iterator value="allemployees"> <s:property value="name"/>, <s:property value="department"/>

append標籤需要兩個或兩個以上列出作爲參數。我們需要給予追加一個id,這樣我們就可以重用它。在這個例子中,我們提供了作爲參數傳遞給員工和承包商的附加標籤。然後,我們使用「allemployees」ID遍歷附加列表和打印員工的細節。

配置文件

struts.xml中應該像這樣:

<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

web.xml中,應該像這樣:

<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"> Struts 2 index.jsp struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /*

右鍵點擊項目名稱,並單擊Export > WAR File 創建一個WAR文件。然後部署此WAR在Tomcat的webapps目錄下。最後,啓動Tomcat服務器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/employee.action。這會給出以下畫面:

Struts