Structs2入門一

一、Struts2框架介紹
1、三大框架 : 是企業主流 JavaEE 開發的一套架構
Struts2 + Spring + Hibernate

2、 什麼是框架?爲什麼學習框架?
框架 是 實現部分功能的代碼 (半成品),使用框架簡化企業級軟件開發
提高開發效率,降低代碼的耦合度等。

3、 什麼是Struts2 ?
Struts2 是一款優秀MVC框架

MVC:是一種思想,是一種模式,將軟件分爲 Model模型、View視圖、Controller控制器
* MVC由來是web開發

JavaEE軟件三層結構 : web層(表現層)、業務邏輯層、數據持久層 (sun提供JavaEE開發規範)
JavaEE開發更強調三層結構, web層開發注重MVC

struts2 就是 web層開發框架,符合MVC模式
* struts1 、webwork 、jsf 、SpringMVC 都是MVC

4、 Struts2 和 Struts1 關係
沒有關係, Struts2 全新框架,引入WebWork很多技術和思想,Struts2 保留Struts1 類似開發流程
* Struts2 內核 webwork

Xwork提供了很多核心功能:前端攔截機(interceptor),運行時表單屬性驗證,類型轉換,強大的表達式語言(OGNL – the Object Graph Navigation Language),IoC(Inversion of Control反轉控制)容器等
Struts2快速入門
1、 下載開發包:以 struts2 3.15.1 爲例

2、 Struts2目錄結構
apps : struts2官方demo
docs : 文檔
lib : jar包
src : 源碼

3、 導入jar包到開發工程
只需要導入 apps/struts2-blank.war 中所有jar包 —- 13 個jar包
不需要記住是幾個jar包,每個版本的struct2的jar包也許都不一樣,只需要去 apps/struts2-blank.war 找,就是需要的最少的jar包

4、 編寫頁面
hello.jsp 請求頁面
訪問struts2入門

success.jsp 結果頁面

5、在web.xml 配置struts2 前端控制器 (Filter)

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>```

6、執行struts2過濾器後,讀取struts2配置文件,將請求分發
在src下創建struts.xml 







“`

7、執行目標Action中execute方法

8、在Action的execute方法中返回 字符串,在struts.xml中配置字符串與頁面對應關係
/demo1/success.jsp 完成結果頁面跳轉
Struts2流程分析與工具配置
1、 運行流程
請求 —- StrutsPrepareAndExecuteFilter 核心控制器 —– Interceptors 攔截器(實現代碼功能 ) —– Action 的execuute — 結果頁面 Result
* 攔截器 在 struts-default.xml定義
* 執行攔截器 是 defaultStack 中引用攔截器

—- 通過源代碼級別斷點調試,證明攔截器是執行

2、 配置struts.xml 提示問題
如果安裝Aptana編輯器 ,請不要用Aptana自帶xml編輯器 編寫struts2配置文件
struts.xml提示來自於 DTD約束,
“-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
“http://struts.apache.org/dtds/struts-2.3.dtd”>
如果可以上網,自動緩存dtd,提供提示功能
如果不能上網,也可以配置本地DTD提示

* 導入DTD時,應該和配置DTD版本一致

3、 關聯struts2源碼
關聯 zip包

4、 Config Brower 插件使用
提供在瀏覽器中查看 struts2 配置加載情況

將解壓struts2/lib/struts2-config-browser-plugin-2.3.7.jar 複製WEB-INF/lib下

訪問 http://localhost:8080/struts2_day1/config-browser/index.action 查看 struts2配置加載情況
Struts2配置
學習路徑
1)、 struts.xml常量配置(配置文件順序)、Action訪問(Servlet API)、結果集 (使用Struts2 編寫簡單案例)
2)、 請求數據
3)、 響應頁面生成
Struts2配置文件加載順序
struts2 配置文件 由核心控制器加載 StrutsPrepareAndExecuteFilter (預處理,執行過濾)
init_DefaultProperties(); // [1] ———- org/apache/struts2/default.properties
init_TraditionalXmlConfigurations(); // [2] — struts-default.xml,struts-plugin.xml,struts.xml
init_LegacyStrutsProperties(); // [3] — 自定義struts.properties
init_CustomConfigurationProviders(); // [5] —– 自定義配置提供
init_FilterInitParameters() ; // [6] —– web.xml
init_AliasStandardObjects() ; // [7] —- Bean加載

結論 :
default.properties 該文件保存在 struts2-core-2.3.7.jar 中 org.apache.struts2包裏面 (常量的默認值)
struts-default.xml 該文件保存在 struts2-core-2.3.7.jar (Bean、攔截器、結果類型 )
struts-plugin.xml 該文件保存在struts-Xxx-2.3.7.jar (在插件包中存在 ,配置插件信息 )

struts.xml 該文件是web應用默認的struts配置文件 (實際開發中,通常寫struts.xml ) ********************
struts.properties 該文件是Struts的默認配置文件 (配置常量 )
web.xml 該文件是Web應用的配置文件 (配置常量 )

  • 後加載文件中struts2 常量會覆蓋之前加載文件 常量內容
    Struts2的Action配置
    1)必須要爲元素 配置元素 (struts2 圍繞package進行Action的相關配置 )
    配置package 三個常用屬性

    name 包名稱,在struts2的配置文件文件中 包名不能重複 ,name並不是真正包名,只是爲了管理Action
    namespace 和 的name屬性,決定 Action的訪問路徑 (以/開始 )
    extends 繼承哪個包,通常開發中繼承 struts-default 包 (struts-default包在 struts-default.xml定義 )
    * 繼承struts-default包後,可以使用 包中定義攔截器和結果類型
    2)Action的通過元素配置

    的name 和 的namespace屬性 共同決定 Action的訪問路徑 !!!!!!!!

    例如 :


    訪問路徑 /user/hello.action
    3) 元素配置默認值
    的namespace 默認值 “”
    的class 默認值 ActionSupport 類
    的 name 默認值 success

默認Action 和 Action的默認處理類
1) 默認Action , 解決客戶端訪問Action不存在的問題 ,客戶端訪問Action, Action找不到,默認Action 就會執行

2) 默認處理類 ,客戶端訪問Action,已經找到匹配元素,但是元素沒有class屬性,執行默認處理類

* 在struts-default.xml 配置默認處理類 ActionSupport
Struts2的常量配置
1) struts2 默認常量 在 default.properties 中配置
2) 開發者自定義常量
struts.xml (要求)
格式 :
struts.properties (要求)
格式 : struts.devMode = true
web.xml
格式 :

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts.devMode
true

3) 常用常量
—– 相當於request.setCharacterEncoding(“UTF-8”); 解決post請求亂碼
— 訪問struts2框架Action訪問路徑 擴展名 (要求)
* struts.action.extension=action,, 默認以.action結尾擴展名 和 不寫擴展名 都會分發給 Action
false不緩存,true瀏覽器會緩存靜態內容,產品環境設置true、開發環境設置false
提供詳細報錯頁面,修改struts.xml後不需要重啓服務器 (要求)
struts2 配置文件分離
通過 將struts2 配置文件 拆分
Action
HTTP請求 提交 Struts2 StrutsPrepareAndExecuteFilter 核心控制器 —— 請求分發給不同Action
Action書寫的的三種格式
第一種 Action可以是 POJO ((PlainOldJavaObjects)簡單的Java對象) —- 不需要繼承任何父類,實現任何接口
* struts2框架 讀取struts.xml 獲得 完整Action類名
* obj = Class.forName(“完整類名”).newInstance();
* Method m = Class.forName(“完整類名”).getMethod(“execute”); m.invoke(obj); 通過反射 執行 execute方法

第二種 編寫Action 實現Action接口
Action接口中,定義默認五種 邏輯視圖名稱
public static final String SUCCESS = “success”; // 數據處理成功 (成功頁面)
public static final String NONE = “none”; // 頁面不跳轉 return null; 效果一樣
public static final String ERROR = “error”; // 數據處理發送錯誤 (錯誤頁面)
public static final String INPUT = “input”; // 用戶輸入數據有誤,通常用於表單數據校驗 (輸入頁面)
public static final String LOGIN = “login”; // 主要權限認證 (登陸頁面)

  • 五種邏輯視圖,解決Action處理數據後,跳轉頁面

第三種 編寫Action 繼承ActionSupport (推薦)
在Action中使用 表單校驗、錯誤信息設置、讀取國際化信息 三個功能
Action的配置method(通配符)
1) 在配置 元素時,沒有指定method屬性, 默認執行 Action類中 execute方法

2) 在 元素內部 添加 method屬性,指定執行Action中哪個方法
執行 RegistAction 的regist方法
* 將多個請求 業務方法 寫入到一個Action 類中

3) 使用通配符* ,簡化struts.xml配置
添加客戶
刪除客戶

struts.xml
<action name="customer_*" class="cn.itcast.struts2.demo4.CustomerAction" method="{1}"></action>   ---  {1}就是第一個* 匹配內容

動態方法調用
訪問Action中指定方法,不進行配置
1) 在工程中使用 動態方法調用 ,必須保證 struts.enable.DynamicMethodInvocation = true 常量值 爲true
2) 在action的訪問路徑 中 使用 “!方法名”
頁面
添加商品
配置

執行 ProductAction 中的 add方法
“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章