Struts2是一個基于MVC設(shè)計模式的Web應(yīng)用框架,它本質(zhì)上相當(dāng)于一個servlet,在MVC設(shè)計模式中,Struts2作為控制器(Controller)來建立模型與視圖的數(shù)據(jù)交互。
1.MVC 設(shè)計模式
- MVC 是 Model-View-Control 的簡稱,即模型-視圖-控制器。它是一個存在于web應(yīng)用中的表現(xiàn)層的模型,它將應(yīng)用分開,改變應(yīng)用之間的高度耦合,進(jìn)行對程序的擴(kuò)展性以及后期維護(hù)成本降低
- MVC 模式將應(yīng)用分為模型、視圖和控制器三個部分:
- 視圖:數(shù)據(jù)的展現(xiàn)。 JSP 、 html 、 freemarker、 pdf 、 execl等
視圖是用戶看到并與之交互的界面。視圖向用戶顯示相關(guān)的數(shù)據(jù),并能接收用戶的輸入數(shù)據(jù),但是它并不進(jìn)行任何實際的業(yè)務(wù)處理。視圖可以向模型查詢業(yè)務(wù)狀態(tài),但不能改變模型。視圖還能接受模型發(fā)出的數(shù)據(jù)更新事件,從而對用戶界面進(jìn)行同步更新。 - 模型:應(yīng)用對象(數(shù)據(jù)模型、業(yè)務(wù)模型)。 JAVABEAN( DOMAIN、S ERVCIE 包下面的java 類)
模型是應(yīng)用程序的主體部分。 模型代表了業(yè)務(wù)數(shù)據(jù)和業(yè)務(wù)邏輯; 當(dāng)數(shù)據(jù)發(fā)生改變時,它要負(fù)責(zé)通知視圖部分;一個模型能為多個視圖提供數(shù)據(jù)。由于同一個模型可以被多個視圖重用,所以提高了應(yīng)用的可重用性。 -
控制器:邏輯處理、控制實體數(shù)據(jù)在視圖上展示、調(diào)用模型處理業(yè)務(wù)請求。 (servlet 、 filter 充當(dāng)控制器)
當(dāng) Web 用戶單擊 Web 頁面中的提交按鈕來發(fā)送 HTML 表單時,控制器接收請求并調(diào)用相應(yīng)的模型組件去處理請求,然后調(diào)用相應(yīng)的視圖來顯示模型返回的數(shù)據(jù)。
Mvc模型圖:
圖片.png
2.三層架構(gòu)
所謂三層體系結(jié)構(gòu),是在客戶端與數(shù)據(jù)庫之間加入了一個“中間層”,也叫組件層。這里所說的三層體系,不是指物理上的三層,不是簡單地放置三臺機(jī)器就是三層體系結(jié)構(gòu),也不僅僅有B/S應(yīng)用才是三層體系結(jié)構(gòu),三層是指邏輯上的三層,即使這三個層放置到一臺機(jī)器上。 三層體系的應(yīng)用程序?qū)I(yè)務(wù)規(guī)則、數(shù)據(jù)訪問、合法性校驗等工作放到了中間層進(jìn)行處理。通常情況下,客戶端不直接與數(shù)據(jù)庫進(jìn)行交互,而是通過http協(xié)議和web服務(wù)器建立連接,找到程序中間層與數(shù)據(jù)庫進(jìn)行交互。
?三層架構(gòu)的優(yōu)點:
優(yōu)點:開發(fā)人員可以只關(guān)注整個結(jié)構(gòu)中的其中某一層;
可以很容易的用新的實現(xiàn)來替換原有層次的實現(xiàn);
可以降低層與層之間的依賴;
有利于標(biāo)準(zhǔn)化;
利于各層邏輯的復(fù)用。
缺點:
降低了系統(tǒng)的性能。這是不言而喻的。如果不采用分層式結(jié)構(gòu),很多業(yè)務(wù)可以直接造訪數(shù)據(jù)庫,以此獲取相應(yīng)的數(shù)據(jù),如今卻必須通過中間層來完成。
有時會導(dǎo)致級聯(lián)的修改。這種修改尤其體現(xiàn)在自上而下的方向。如果在表示層中需要增加一個功能,為保證其設(shè)計符合分層式結(jié)構(gòu),可能需要在相應(yīng)的業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層中都增加相應(yīng)的代碼。增加了開發(fā)成本。

3.Struts2 歷史
- 框架的理解:框架其實是一個半成品的程序,業(yè)務(wù)需求需要程序員去實現(xiàn),模板性的功能及代碼,框架進(jìn)行了有效的封裝,提高了程序員開發(fā)的效率,把更多的精力集中到了業(yè)務(wù)需求的開發(fā)上來。
- Struts2框架在屬于表現(xiàn)層的框架(三層架構(gòu)的表現(xiàn)層)
- Struts2框架基于MVC的設(shè)計模式實現(xiàn)
- Struts1與Struts2的關(guān)系:嚴(yán)格上來講,Struts2不是Struts1的升級版,Struts2=Struts1+webwork的結(jié)合體,吸取兩個框架的有點而形成
早期現(xiàn)有struts1,struts1和struts2都是apache的產(chǎn)品,struts1在2003年左右比較火.在當(dāng)時,表現(xiàn)層框架有很多:webwork框架。
- Struts1 vs webwork 區(qū)別:
Struts1比較出名因為后臺是apache,webwork不是很出名
Webwork框架的設(shè)計思想比較前衛(wèi)(AOP),設(shè)計思想沿用至今,struts1的耦合性(servlet api)很緊密,入侵性,就導(dǎo)致使用struts1擴(kuò)展性差

4.第一個Struts2 小程序
- 導(dǎo)包:
asm-5.2.jar
asm-commons-5.2.jar
asm-tree-5.2.jar
commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang3-3.6.jar
freemarker-2.3.26-incubating.jar
javassist-3.20.0-GA.jar
jsp-api.jar
log4j-api-2.9.1.jar
log4j-core-2.9.1.jar
ognl-3.1.15.jar
servlet-api.jar
struts2-core-2.5.14.1.jar
struts2-core-2.5.14.1.jar - 配置 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"
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">
<!-- 配置一個 過濾器:該過濾器是 struts2 提供的 過濾器 :StrutsPrepareAndExecuteFilter (前端控制器、中央控制器)
在 玩 struts2 的使用認(rèn)識兩種控制器 : StrutsPrepareAndExecuteFilter
控制器(頁面(邏輯)控制器): 自定義的
-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
- 創(chuàng)建 struts.xml 配置文件,該文件名稱不能變,放置的路徑一定在是class 路徑的頂端
-
配置的沒有提示的問題,解決如下,
圖片.png
圖片.png
圖片.png
-
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="p1" extends="struts-default" namespace="/hello">
<action name="struts2" class="com.xingxue.xingxue.web.controller.HelloController">
<result name="ok">
/ok.jsp
</result>
</action>
</package>
</struts>
- 編寫了一個 頁面控制器 Controller
package com.xingxue.xingxue.web.controller;
public class HelloController {
public String execute() {
System.out.println("execute 方法執(zhí)行了...");
return "ok";
}
}
5.Struts2 執(zhí)行流程
-
簡易圖
圖片.png -
官方圖:
圖片.png
6.Action 動作類創(chuàng)建三種方式
- 第一種方式
public class HelloController {
- 第二種方式: 實現(xiàn)Action 接口
提供了一個 execute 方法
提供了4個常量:
/**
* The action execution was successful. Show result
* view to the end user.
*/
public static final String SUCCESS = "success";
/**
* The action execution was successful but do not
* show a view. This is useful for actions that are
* handling the view in another fashion like redirect.
*/
public static final String NONE = "none";
/**
* The action execution was a failure.
* Show an error view, possibly asking the
* user to retry entering data.
*/
public static final String ERROR = "error";
/**
* <p>
* The action execution require more input
* in order to succeed.
* This result is typically used if a form
* handling action has been executed so as
* to provide defaults for a form. The
* form associated with the handler should be
* shown to the end user.
* </p>
*
* <p>
* This result is also used if the given input
* params are invalid, meaning the user
* should try providing input again.
* </p>
*/
public static final String INPUT = "input";
/**
* The action could not execute, since the
* user most was not logged in. The login view
* should be shown.
*/
public static final String LOGIN = "login";
public class HelloController2 implements Action {
@Override
public String execute() throws Exception {
return null;
}
- 第三方式: 繼承ActionSupport ,可以使用 Struts2 提供的 國際化功能以及 數(shù)據(jù)校驗功能
public class HelloController3 extends ActionSupport {
}
7.接收請求數(shù)據(jù)
- 模型不分離:直接在 動作類的成員位置定義變量來接收
public class RequestController {
// 動作類的方法:1,修飾符是 public 2.動作方法沒有形參,3.方法的返回值有 String 或者 void
public String name;
public String password;
- 模型分離 封裝數(shù)據(jù)從動作類封裝變?yōu)橛媚P蛠矸庋b
public class RequestController {
// 動作類的方法:1,修飾符是 public 2.動作方法沒有形參,3.方法的返回值有 String 或者 void
// public String name;
// public String password;
// 定義接收參數(shù)的模型
public UserModel model = new UserModel();
<a href="${pageContext.request.contextPath }/p1/req2.action?model.name=lisi&model.password=123">a標(biāo)簽提交請求參數(shù)</a>
8.動態(tài)視圖數(shù)據(jù)處理
處理的數(shù)據(jù)帶到頁面的操作
- servlet api 緊耦合版
ServletActionContext:
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ServletContext servletContext = ServletActionContext.getServletContext();
- 松耦合版 (推薦使用)
ServletActionContext:
ActionContext context = ActionContext.getContext();
// 請求域中放數(shù)據(jù)
context.put("", "");
Map<String, Object> session = context.getSession();
Map<String, Object> application = context.getApplication();





