- Struts 2 教程
- Struts2 - 首頁
- Struts2 - 基本MVC架構
- Struts2 - 概述
- Struts2 - 環境搭建
- Struts2 - 架構
- Struts2 - 示例
- Struts2 - 配置
- Struts2 - Action
- Struts2 - 攔截器
- Struts2 - 結果型別
- Struts2 - 值棧/OGNL
- Struts2 - 檔案上傳
- Struts2 - 資料庫訪問
- Struts2 - 傳送郵件
- Struts2 - 驗證
- Struts2 - 國際化
- Struts2 - 型別轉換
- Struts2 - 主題/模板
- Struts2 - 異常處理
- Struts2 - 註解
- Struts 2 標籤
- Struts2 - 控制標籤
- Struts2 - 資料標籤
- Struts2 - 表單標籤
- Struts2 - Ajax 標籤
- Struts 2 整合
- Struts2 - Spring
- Struts2 - Tiles
- Struts2 - Hibernate
- Struts 2 有用資源
- Struts2 - 問答
- Struts2 - 快速指南
- Struts2 - 有用資源
- Struts2 - 討論
Struts 2 & Spring 整合
Spring是一個流行的Web框架,它簡化了許多常見的Web任務的整合。所以問題是,當我們有了Struts2後,為什麼還需要Spring? 嗯,Spring不僅僅是一個MVC框架——它提供了許多Struts中沒有的其他功能。
例如:依賴注入,這對於任何框架都很有用。在本章中,我們將透過一個簡單的示例來了解如何將Spring和Struts2整合在一起。
首先,您需要將以下檔案從Spring安裝目錄新增到專案的構建路徑。您可以從https://www.springsource.org/download下載並安裝最新版本的Spring框架
- org.springframework.asm-x.y.z.M(a).jar
- org.springframework.beans-x.y.z.M(a).jar
- org.springframework.context-x.y.z.M(a).jar
- org.springframework.core-x.y.z.M(a).jar
- org.springframework.expression-x.y.z.M(a).jar
- org.springframework.web-x.y.z.M(a).jar
- org.springframework.web.servlet-x.y.z.M(a).jar
最後,將struts2-spring-plugin-x.y.z.jar新增到您的WEB-INF/lib目錄中(位於您的Struts庫目錄)。如果您使用的是Eclipse,則可能會遇到異常java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener。
要解決此問題,您需要轉到“標記”選項卡,依次右鍵單擊類依賴項,然後執行快速修復以釋出/匯出所有依賴項。最後,確保“標記”選項卡下沒有依賴衝突。
現在,讓我們按照如下方式設定Struts-Spring整合的web.xml:
<?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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
這裡需要注意的重要一點是我們配置的監聽器。ContextLoaderListener 負責載入Spring上下文檔案。Spring的配置檔名為applicationContext.xml,它必須與web.xml檔案位於同一級別。
讓我們建立一個名為User.java的簡單Action類,它包含兩個屬性:firstName和lastName。
package com.tutorialspoint.struts2;
public class User {
private String firstName;
private String lastName;
public String execute() {
return "success";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
現在讓我們建立Spring配置檔案applicationContext.xml並例項化User.java類。如前所述,此檔案應位於WEB-INF資料夾下:
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "userClass" class = "com.tutorialspoint.struts2.User">
<property name = "firstName" value = "Michael" />
<property name = "lastName" value = "Jackson" />
</bean>
</beans>
如上所示,我們配置了user bean,並將值Michael和Jackson注入到bean中。我們還為此bean命名為“userClass”,以便可以在其他地方重用它。接下來,讓我們在WebContent資料夾中建立User.jsp:
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2 - Spring integration</h1>
<s:form>
<s:textfield name = "firstName" label = "First Name"/><br/>
<s:textfield name = "lastName" label = "Last Name"/><br/>
</s:form>
</body>
</html>
User.jsp檔案非常簡單。它只有一個用途——顯示user物件的firstName和lastName的值。最後,讓我們使用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 = "helloworld" extends = "struts-default">
<action name = "user" class="userClass"
method = "execute">
<result name = "success">/User.jsp</result>
</action>
</package>
</struts>
需要注意的重要一點是,我們使用id userClass來引用類。這意味著我們使用Spring為User類進行依賴注入。
現在右鍵單擊專案名稱,然後單擊匯出 > WAR檔案來建立一個WAR檔案。然後將此WAR檔案部署到Tomcat的webapps目錄。最後,啟動Tomcat伺服器並嘗試訪問URL https://:8080/HelloWorldStruts2/User.jsp。這將生成以下螢幕:
我們現在已經瞭解瞭如何將兩個優秀的框架結合起來。Struts-Spring整合的章節到此結束。