標籤:http java 使用 os 檔案 資料
1 struts架構入門:
1 jsp頁面:
hello.jsp:<a href="${pageContext.request.contextPath}/hello.action">struts入門</a>
success.jsp:結果處理頁面
2 web.xml中配置前端配置器:
</welcome-file-list>
<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>
3 struts設定檔:將請求分發
<struts>
<constant value="false" name="struts.enable.DynamicMethodInvocation"/>
<constant value="false" name="struts.devMode"/>
<package name="default" namespace="/" extends="struts-default">
<!-- <a href="${pageContext.request.contextPath }/hello.action">訪問struts2入門</a> -->
<!-- 將請求 分發給一個Action -->
<!-- action的name 就是hello.action 去掉副檔名 -->
<action name="hello" class="cn.itcast.struts2.demo1.HelloAction">
<result name="hehe">/success.jsp</result>
</action>
</package>
</struts>
4 java類:
HelloAction:
package cn.itcast.struts2.demo1;
public class HelloAction {
public String execute(){
System.out.println("hello");
return "hehe";
}
}
config Brower 外掛程式的的使用
(略,簡單瞭解)
2 struts2 運行流程分析
1、 運行流程
請求 ---- StrutsPrepareAndExecuteFilter 核心控制器 ----- Interceptors 攔截器(實現代碼功能 ) ----- Action 的execuute --- 結果頁面 Result
* 攔截器 在 struts-default.xml定義
* 執行攔截器 是 defaultStack 中引用攔截器
---- 通過原始碼層級斷點調試,證明攔截器是執行
2、 配置struts.xml 提示問題
如果安裝Aptana編輯器 ,請不要用Aptana內建xml編輯器 編寫struts2設定檔
struts.xml提示來自於 DTD約束,
<!DOCTYPE struts PUBLIC
"-//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)、 響應頁面產生
1、 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 常量會覆蓋之前負載檔案 常量內容
3 struts.xml完成Action 相關配置
1)必須要為<action>元素 配置<package>元素 (struts2 圍繞package進行Action的相關配置 )
配置package 三個常用屬性
<package name="default" namespace="/" extends="struts-default">
name 包名稱,在struts2的設定檔檔案中 包名不能重複 ,name並不是真正包名,只是為了管理Action
namespace 和 <action>的name屬性,決定 Action的訪問路徑 (以/開始 )
extends 繼承哪個包,通常開發中繼承 struts-default 包 (struts-default包在 struts-default.xml定義 )
* 繼承struts-default包後,可以使用 包中定義攔截器和結果類型
2)Action的通過<action>元素配置
<action name="hello" class="cn.itcast.struts2.demo1.HelloAction">
<action>的name 和 <package>的namespace屬性 共同決定 Action的訪問路徑 !!!!!!!!
例如 :
<package name="default" namespace="/user" extends="struts-default">
<action name="hello" class="cn.itcast.struts2.demo1.HelloAction">
訪問路徑 /user/hello.action
3) <action> 元素配置預設值
<package> 的namespace 預設值 /
<action> 的class 預設值 ActionSupport 類
<result> 的 name 預設值 success
3 預設Action 和 Action的預設處理類
1) 預設Action , 解決用戶端訪問Action不存在的問題 ,用戶端訪問Action, Action找不到,預設Action 就會執行
<default-action-ref name="action元素的name" />
2) 預設處理類 ,用戶端訪問Action,已經找到匹配<action>元素,但是<action>元素沒有class屬性,執行預設處理類
<default-class-ref class="完成類名" />
* 在struts-default.xml 配置預設處理類 ActionSupport
配置預設action代碼:
<action name="hello" class="cn.itcast.struts2.demo1.HelloAction">
<result name="hehe">/success.jsp</result>
</action>
<default-action-ref name="error"></default-action-ref>
<action name="error" class="處理類地址"></action>
4 常用常量
<constant name="struts.i18n.encoding" value="UTF-8"/> ----- 相當於request.setCharacterEncoding("UTF-8"); 解決post請求亂碼
<constant name="struts.action.extension" value="action"/> --- 訪問struts2架構Action訪問路徑 副檔名 (要求)
* struts.action.extension=action,, 預設以.action結尾副檔名 和 不寫副檔名 都會分發給 Action
<constant name="struts.serve.static.browserCache" value="false"/> false不緩衝,true瀏覽器會緩衝靜態內容,產品環境設定true、開發環境設定false
<constant name="struts.devMode" value="true" /> 提供詳細報錯頁面,修改struts.xml後不需要重啟伺服器 (要求)
5、 struts2 設定檔分離
通過 <include file="struts-part1.xml"/> 將struts2 設定檔 拆分
6 Action的訪問
HTTP請求 提交 Struts2 StrutsPrepareAndExecuteFilter 核心控制器 ------ 請求分發給不同Action
1、 讓請求能夠訪問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中使用 表單校正、錯誤資訊設定、讀取國際化資訊 三個功能
7 action方法的調用
1) 在配置 <action> 元素時,沒有指定method屬性, 預設執行 Action類中 execute方法
<action name="request1" class="cn.itcast.struts2.demo3.RequestAction1" />
2) 在<action> 元素內部 添加 method屬性,指定執行Action中哪個方法
<action name="regist" class="cn.itcast.struts2.demo4.RegistAction" method="regist"/> 執行 RegistAction 的regist方法
* 將多個請求 業務方法 寫入到一個Action 類中
<action name="addBook" class="cn.itcast.struts2.demo4.BookAction" method="addBook" ></action>
<action name="delBook" class="cn.itcast.struts2.demo4.BookAction" method="delBook" ></action>
3) 使用萬用字元* ,簡化struts.xml配置
<a href="${pageContext.request.contextPath }/user/customer_add.action">添加客戶</a>
<a href="${pageContext.request.contextPath }/user/customer_del.action">刪除客戶</a>
struts.xml
<action name="customer_*" class="cn.itcast.struts2.demo4.CustomerAction" method="{1}"></action> --- {1}就是第一個* 匹配內容
動態方法引動過程 (零配置路線)
訪問Action中指定方法,不進行配置
1) 在工程中使用 動態方法引動過程 ,必須保證 struts.enable.DynamicMethodInvocation = true 常量值 為true
2) 在action的訪問路徑 中 使用 "!方法名"
頁面
<a href="${pageContext.request.contextPath }/user/product!add.action">添加商品</a>
配置
<action name="product" class="cn.itcast.struts2.demo4.ProductAction"></action>
執行 ProductAction 中的 add方法
8 在Action中使用Servlet API
1、 在Action 中解耦合方式 間接訪問 Servlet API --------- 使用 ActionContext 對象
在struts2 中 Action API 已經與 Servlet API 解耦合 (沒有依賴關係 )
* Servlet API 常見操作 : 表單提交請求參數擷取,向request、session、application三個範圍存取資料
actionContext = ActionContext.getContext();
1) actionContext.getParameters(); 獲得所有請求參數Map集合
2) actionContext.put("company", "傳智播客"); / actionContext.get("company") 對request範圍存取資料
3) actionContext.getSession(); 獲得session資料Map,對Session範圍存取資料
4) actionContext.getApplication(); 獲得ServletContext資料Map,對應用訪問存取資料
2、 使用介面注入的方式,操作Servlet API (耦合)
ServletContextAware : 注入ServletContext對象
ServletRequestAware :注入 request對象
ServletResponseAware : 注入response對象
* 程式要使用哪個Servlet的對象,實現對應介面
3、 在Action中直接通過 ServletActionContext 獲得Servlet API
ServletActionContext.getRequest() : 獲得request對象 (session)
ServletActionContext.getResponse() : 獲得response 對象
ServletActionContext.getServletContext() : 獲得ServletContext對象
* 靜態方法沒有線程問題,ThreadLocal
約定和註解:
1、 約定 struts2提供預設規則,實現零配置
1)匯入jar包 11個jar + struts2-convention-plugin-2.3.7.jar
2)在web.xml 配置前端控制器
3)編寫頁面
4)外掛程式中 plugin設定檔
<constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/>
編寫Action類,必須位於 action,actions,struts,struts2 四個包中
<constant name="struts.convention.action.suffix" value="Action"/>
以Action結尾
**** <constant name="struts.convention.result.path" value="/WEB-INF/content/"/> 結果result頁面存放位置
Action被掃描後,如何確定Action的訪問路徑的 ?
cn.itcast.struts2.HelloAction (HelloAction位於直接位於四個掃描包下,namespace是/,Action的name是hello) ---- /hello.action
cn.itcast.actions.books.BookSearchAction (BookSearchAction 不是直接位於四個掃描包下,namespace是/books, Action的name是book-search)
* 訪問路徑 /books/book-search.action
cn.itcast.struts.user.UserAction 訪問 /user/user.action
cn.itcast.estore.action.test.LoginAction 訪問 /test/login.action
5) 根據常量配置 結果頁面 位於 /WEB-INF/content下
頁面命名規則約定: actionName + resultCode + suffix
例如: cn.itcast.struts.user.UserAction ------ /user/user.action 返回 SUCCESS
結果頁面 /WEB-INF/content/user/user-success.jsp --- 找不到 /WEB-INF/content/user/user-success.html --- 找不到 /WEB-INF/content/user/user.jsp
2、 註解
註解開發第一步 基於約定的自動掃描
約定只解決Action訪問和結果頁面跳轉問題
* 在開發中需要為Action指定攔截器,進行更細節result配置
* 約定不夠靈活,註解的功能 是和 xml配置方式 等價的
使用 @Action註解配置訪問路徑 @Result註解 配置結果頁面 :
代碼體現:
@Action(value="login",results=(@Result(name="success",location="/index.jsp")))
Action類檔案重新自動載入:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.convention.classes.reload</param-name>
<param-value>true</param-value>
</init-param>
</filter>
@ParentPackage 配置<package> extends 繼承哪個包
@Namespace 配置包名稱空間