程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> struts2基礎——請求與響應、獲取web資源,struts2web

struts2基礎——請求與響應、獲取web資源,struts2web

編輯:JAVA綜合教程

struts2基礎——請求與響應、獲取web資源,struts2web


一、請求與響應

Action
1.含義:
(1) struts.xml 中的 action 元素,也指 from 表單的 action 屬性,總之代表一個 struts2 請求。
(2) 用於處理 Struts2 請求的 Action 類

2.Action 類
(1) 使用 setXxx() 方法和 getXxx() 方法定義屬性,使用 setXxx() 屬性方法接受請求參數值,使用 getXxx() 方法來在頁面顯示數據。
(2) 有無參構造器
(3) 至少有一個供 Struts2 在執行這個 action 時調用的方法
(4) 同一個 Action 來可以包含多個 action 方法
(5) Action 類不是單例的,Struts2 為每一個請求創建一個 Action 實例

3.ActionSupport 類
(1) ActionSupport 是默認的 Action 類
(2) ActionSupport 實現的接口

Action:提供了 SUCCESS、INPUT 等字符串常量可以直接使用,提供 execute() 抽象方法供實現
Validateable:用於編程式輸入驗證
ValidationAware:用於獲取和顯示錯誤消息,錯誤消息有兩個級別:一個是類級別,另一個是字段級別
TextProvider:從資源文件中讀取屬性值,用於國際化
LocaleProvider:獲取 Locale 對象,用於國際化
Serializable:序列化 Action 類

4.請求擴展名
(1) 請求路徑中 ServletPath 部分包含的後綴,例如:.action/.do
(2) Struts2 根據擴展名來區分哪些請求需要 Struts2 處理,哪些不需要。
(3) 默認支持的擴展名: .action 和 沒有

5.default.properties
在 default.properties 定義了許多常量,如默認支持的請求擴展名
修改 default.properties 定義的常量:
在 struts 根標簽下加入元素:
<constant name="" value=""/>
如修改默認支持的請求擴展名:
<constant name="struts.action.extension" value=".action,,.do"/>


Result:
1.含義:
(1) 代表 Struts2 請求的響應,每個 action 標簽可以包含多個 result 元素
(2) result 標簽:name 屬性:對應 action 方法的 String 類型方法返回值,type 屬性:執行結果類型,以什麼方式跳轉。

2.結果類型:
(1) Struts2 在 struts-default.xml 中定義了 10 結果類型

<result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> </result-types> result types

常用的有5個類型:chain,dispatcher,redirect,redirectAction,stream

(2) 常用結果類型詳解
dispatcher:struts2默認的結果類型,轉發到目標資源,但是不能是一個 Action,因為進行內部轉發的時候,struts2 無法進行攔截。

chain:轉發到一個 Action ,如:

<action name="hello" class="com.nucsoft.struts2.helloworld.HelloWorld" method="secondMethod"> <result type="chain"> <param name="actionName">worldhello</param> <param name="namespace">/</param> </result> </action> chain

redirect:重定向到目標資源

<result name="redirect" type="redirect">/result.jsp</result> redirect

這裡指定目標資源的虛擬路徑時,不能包含 web 應用的虛擬路徑,目標資源也可以是一個 Action。

redirectAction:重定向到一個 Action

<action name="helloworld_*" class="com.nucsoft.struts2.helloworld.HelloWorld" method="{1}"> <result>/success.jsp</result> <result type="redirectAction" name="redirectAction"> <param name="actionName">redirect2Action</param> </result> </action> <action name="redirect2Action" class="com.nucsoft.struts2.helloworld.HelloWorld" method="redirectAction"> <result>/success.jsp</result> </action> redirectAction

stream:
以輸出流的形式返回響應結果,用於文件的下載和 Ajax


通配符映射規則:
1.精確匹配優先。
2.在不符合精確匹配優先的情況下,則先聲明的有效(如果一個請求能夠與多個帶有通配符的ActionName匹配,則先聲明的有效)。


動態方法調用:
在URL地址中動態調用 Action 中的方法。
Struts2 默認是禁止動態方法調用的,可以通過修改常量的方式修改。
正常訪問:http://localhost:8989/Web應用虛擬路徑/dynamicAction.action
動態方法調用:http://localhost:8989/Web應用虛擬路徑/dynamicAction!dynamicMethod.action

 

二、web 資源
Struts2 針對常用數據進行了封裝,封裝為一系列 Map 對象: RequestMap HashMap SessionMap ApplicationMap

獲取方式:
1.與 Servlet 解耦
(1) 使用 ActionContext 類,通過 ActionContext 對象可以獲取到 appMap、sessionMap、paramMap對象
(2) 實現 XxxAware 接口,RequestAware,ParameterAware,SessionAware,ApplicationAware

2.與 Servlet 耦合
(1) 使用 ServletActionContext 類,通過它獲取到 HttpServletRequest、HttpServletResponse、ServletContext
(2) 實現 ServletXxxAware 接口:ServletRequestAware、ServletResponseAware、ServletContextAware

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved