Struts2中 Result型別配置詳解
在com.opensymphony.xwork2.Action介面中定義了一組標準的結果程式碼,可供開發人員使用,當然了只有我們的action繼承ActionSupport 這個類才可以使用下面的結果程式碼,如下所示:
public interface Action
{
public static final String SUCCESS = “success”;
public static final String NONE = “none”;
public static final String ERROR = “error”;
public static final String INPUT = “input”;
public static final String LOGIN = “login”;
}
其中 Struts2應用在執行過程中若發現addFieldError()中有資訊或者型別轉換失敗或著輸入校驗失敗等情況
那麼它會自動跳轉到name為input的<result/>,然後轉到INPUT所對應的頁面
若JSP頁面中表單是用普通<form>編寫的,發生錯誤而返回該頁面時,則原資料將消失
若JSP頁面中表單是用<s:form/>編寫的,發生錯誤而返回該頁面時,則原資料仍存在
若沒有提供name值為input的<result/>,那麼發生錯誤時,將直接在瀏覽器中提示404錯誤
除了這些預定義的結果碼外,開發人員也可以定義其它的結果碼來滿足自身應用程式的需
要。
Result配置由兩部分組成:一部分是result對映,另一部分是result型別。下面我們分別對
這兩部分進行介紹。
一、配置 result對映
在result對映的配置中,在指定實際資源的位置時,可以使用絕對路徑,也可以使用相對路徑。
絕對路徑以斜槓(/)開頭,相對於當前的Web應用程式的上下文路徑;
相對路徑不以斜槓(/)開頭,相對於當前執行的action的路徑,也就是namespace指定的路徑。
例如:
<package name="default" extends="struts-default" namespace="/admin">
<action name="login" class="com.ibm.LoginAction">
<result>success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
如果當前Web應用程式的上下文路徑是/Shop,那麼請求/Shop/admin/login.action,執行成功後,轉向的頁面路徑為:/Shop/admin/success.jsp;執行失敗後,轉向的頁面路徑為/Shop/error.jsp.
二、result結果型別詳解
type 所有型別 :(在struts2-core.jar/struts-default.xml中可以找到)
Type 型別值 |
作用說明 |
對應類 |
chain |
用來處理Action 鏈 |
com.opensymphony.xwork2.ActionChainResult |
dispatcher(預設值) |
用來轉向頁面,通常處理 JSP |
org.apache.struts2.dispatcher.ServletDispatcherResult |
redirect |
重定向到一個URL |
org.apache.struts2.dispatcher.ServletRedirectResult |
redirectAction |
重定向到一個 Action |
org.apache.struts2.dispatcher.ServletActionRedirectResult |
plainText |
顯示原始檔內容,如檔案原始碼 |
org.apache.struts2.dispatcher.PlainTextResult |
freemarker |
處理 FreeMarker 模板 |
org.apache.struts2.views.freemarker.FreemarkerResult |
httpheader |
控制特殊 http 行為的結果型別 |
org.apache.struts2.dispatcher.HttpHeaderResult |
stream
|
向瀏覽器傳送 InputSream 物件,通常用來處理檔案下載,還可用於返回 AJAX 資料。
|
org.apache.struts2.dispatcher.StreamResult
|
velocity |
處理 Velocity 模板 |
org.apache.struts2.dispatcher.VelocityResult |
xslt |
處理 XML/XLST 模板 |
org.apache.struts2.views.xslt.XSLTResult |
1、dispatcher結果型別
Struts2在後臺使用Servlet API 的RequestDispatcher來轉發請求,因此在使用者的整個請求/響應過程中,目標Servlet/JSP接收到的request/response物件,與最初的Servlet/JSP相同。
Dispatcher結果型別的實現是org.apache.struts2.dispatcher.ServletDispatcherResult,該類的二個屬性(property):location和parse,這兩個屬性可以通過struts.xml配置檔案中的result元素的param子元素來設定。param元素的name屬性指定結果型別實現類的屬性名,param元素的內容是屬性的值。例如:
<result name=“success” type=“dispatcher”>
<param name=“location” >/success.jsp</param>
<param name=“parse” >true</param>
</result>
說明:
A、location引數用於指定action執行完畢後要轉向的目標資源,parse屬性是一個布林型別的值,如果為true,則解析location引數中的OGNL表示式;如果為false,則不解析。parse屬性的預設值就是true.
location引數是預設的引數,在所有的Result實現類中,都定義了一個字串型別的DEFAULT_PARAM靜態常量,專門用於指定預設的引數名。 DEFAULT_PARAM常量的定義:public static final String DEFAULT_PARAM=“location”;
B、在設定location引數時,可以在引數值中使用OGNL表示式。
<action name=“viewNews” class=“com.ibm.ViewNewsAction”
<result name=“success” type=“dispatcher”>
<!--如果引數是中文:請參看最底部例子-->
<param name=“location” >/viewNews.jsp?id=${id}</param>
<param name=“parse” >true</param>
</result>
</action>
考慮到預設值的使用(dispatcher和location都是預設值),上述可以簡化為:
<action name=“viewNews” class=“com.ibm.ViewNewsAction”>
<result name=“success” >viewNews.jsp?id=${id}</result>
</action>
2、redirect結果型別(重定向到一個Url,也可以是Action或一個頁面)
Redirect結果型別在後臺使用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL,它的實現類是org.apache.struts2.dispatcher.ServletRedirectResult.該類同樣有二個屬性(property):location和parse,在使用redirect結果型別的場景中,使用者要完成一次與伺服器之間的互動,瀏覽器需要完成兩次請求,因此第一次請求中的資料在第二次請求中是不可用的,這意味在目標資源中是不能訪問action例項、action錯誤以及錯誤等。
如果有某些資料需要在目標資源中訪問,
i、一種方式是將資料儲存到Session中,
ii、另一種方式是通過請求引數來傳遞資料。
示例(1)、<result name="success" type="redirect">
<param name="location">foo.jsp</param>
<param name="parse">false</param><!--不解析OGNL-->
</result>
示例(2)、
<package name="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">
<-- Passparameters (reportType, width and height),重定向到Url並且傳參 ,如果引數是中文:請參看最底部例子-->
<!--
The redirect-action url generated will be :
/genReport/generateReport.jsp?reportType=pie&width=100&height=100
-->
<actionname="gatherReportInfo" class="...">
<resultname="showReportResult" type="redirect">
<param name="location">generateReport.jsp</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
</result>
</action>
</package>
3、redirectAction結果型別(重定向到一個Action)
他經常用於防止表單重複提交,比方說在增加完使用者之後要顯示列表
redirectAction結果型別的實現類是org.apache.struts2.dispatcher.ServletActionRedirectResult,該類是ServletRedirectResult的子類,因此我們也就可以判斷出redirectAction結果型別和redirect結果型別的後臺工作原理是一樣的,即都是利用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL。
示例、
<package name="public"extends="struts-default">
<action name="login"class="...">
<!--Redirect to another namespace 重定向到不同名稱空間下的 action -->
<result type="redirectAction">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>
<package name="secure"extends="struts-default" namespace="/secure">
<-- Redirectto an action in the same namespace,重定向到同一名稱空間下的action -->
<action name="dashboard" class="...">
<result>dashboard.jsp</result>
<result name="error"type="redirectAction">error</result>
</action>
<action name="error" class="...">
<result>error.jsp</result>
</action>
</package>
<package name="passingRequestParameters"extends="struts-default"namespace="/passingRequestParameters">
<-- Passparameters (reportType, width and height),重定向到Action並且傳參,如果引數是中文:請參看最底部例子 -->
<!--
TheredirectAction url generated will be :
/genReport/generateReport.action?reportType=pie&width=100&height=100
-->
<action name="gatherReportInfo" class="...">
<result name="showReportResult" type="redirectAction">
<param name="actionName">generateReport</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
<param name="empty"></param>
<param name="supressEmptyParameters">true</param>
</result>
</action>
</package>
4、連結型別 result:chain(從一個Action轉發到另一個Action)
chain結果型別有4個屬性,分別是:
actionName (default) - the name of the action that will be chained to
namespace - used to determine which namespace the Action is in that we're chaining. If namespace is null, this defaults to the current namespace
method - used to specify another method on target action to be invoked. If null, this defaults to execute method
skipActions - (optional) the list of comma separated action names for the actions that could be chained to
示例、
<package name="public"extends="struts-default">
<!-- Chain creatAccount to login, using the default parameter ,連結到同一名稱空間下的Action,-->
<action name="createAccount" class="...">
<result type="chain">login</result>
</action>
<actionname="login" class="...">
<!--Chain to another namespace -->
<result type="chain">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>
<package name="secure" extends="struts-default"namespace="/secure">
<actionname="dashboard" class="...">
<result>dashboard.jsp</result>
</action>
</package>
5、HttpHeader Result:HttpHeader(用來控制特殊的Http行為)
httpheader結果型別很少使用到,它實際上是返回一個HTTP響應的頭資訊
示例:<result name="success"type="httpheader">
<paramname="status">204</param>
<paramname="headers.a">a custom header value</param> <paramname="headers.b">another custom header value</param>
</result>
<result name="proxyRequired"type="httpheader">
<paramname="error">305</param>
<paramname="errorMessage">this action must be accessed through aprozy</param>
</result>
6、Stream Result(向瀏覽器傳送InputSream物件,通常用來處理檔案下載)
<result name="success"type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">attachment;filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>
7、PlainText Result(顯示原始檔案內容,例如檔案原始碼)
<action name="displayJspRawContent">
<result type="plaintext">/myJspFile.jsp</result>
</action>
<action name="displayJspRawContent">
<result type="plaintext">
<param name="location">/myJspFile.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>
若僅設定type="plainText"的話,頁面中顯示中文時會亂碼,這時就可以藉助它的charSet屬性以解決中文顯示時的亂碼問題,如果不設定charSet屬性,反而去配置struts.i18n.encoding全域性屬性,是不能解決問題的
設定charSet屬性的目的就是讓JSP頁面的編碼與明文顯示時的編碼一致
8、Velocity Result(處理Velocity模板)
<result name="success"type="velocity">
<paramname="location">foo.vm</param>
</result>
9、XLS Result(處理XML/XLST模板)
<result name="success" type="xslt">
<paramname="location">foo.xslt</param>
<paramname="matchingPattern">^/result/[^/*]$</param>
<paramname="excludingPattern">.*(hugeCollection).*</param>
</result>
10、 FreeMarkerResult (處理FreeMarker模板)
<result name="success"type="freemarker">foo.ftl</result>
附、另外第三方的Result型別還包括JasperReportsPlugin,專門用來處理JasperReport型別的報表輸出。
<%@ tagliburi="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="s"uri="/struts-tags" %>
<%-- Show usage; Used in Header --%>
<tiles:importAttribute name="title"scope="request"/>
<html>
<head><title><tiles:getAsStringname="title"/></title></head>
<body>
<tiles:insertAttribute name="header"/>
<pid="body">
<tiles:insertAttributename="body"/>
</p>
<p>Noticethat this is a layout made in JSP</p>
</body>
</html>
相關文章
- Struts2(六)result 返回型別講解型別
- struts2 result type型別型別
- struts2 action中result引數詳解
- Struts2 OGNL動態配置result(十八)
- Struts2 XML配置詳解XML
- Action裡result型別Stream的引數配置型別
- Swift中Result型別的強大功能Swift型別
- Oracle中number型別詳解Oracle型別
- Java中的Type型別詳解Java型別
- Struts2的properties配置檔案詳解
- JavaScript中的包裝型別詳解JavaScript型別
- 【Struts2】:Struts2的型別轉換型別
- 詳解Apache Hudi如何配置各種型別分割槽Apache型別
- 【SSH2(理論篇)】--Struts2配置詳解
- MySQL 中 blob 和 text 資料型別詳解MySql資料型別
- C#型別詳解C#型別
- Java集合型別詳解Java型別
- Struts2原理詳解
- 處理分頁的result型別問題型別
- JavaScript 資料型別與型別判斷詳解JavaScript資料型別
- SAP 移動型別詳解型別
- JavaScript——資料型別詳解JavaScript資料型別
- MySQL 資料型別詳解MySQL 資料型別
- 詳解MySQL資料型別MySql資料型別
- oracle裡long型別詳解Oracle型別
- C++引用型別詳解C++型別
- MySQL 資料型別 詳解MySQL 資料型別
- number(p,s)型別詳解型別
- struts2標籤詳解 .
- JavaScript 基本資料型別和引用型別的區別詳解JavaScript資料型別
- 關於struts2 action中map型別屬性的對映型別
- JS -- number資料型別詳解JS資料型別
- Java整型資料型別(詳解)Java資料型別
- PostgreSQL 陣列型別使用詳解SQL陣列型別
- 方法中引數的型別詳細型別
- Go 語言型別系統詳解Go型別
- Python資料型別詳解03Python資料型別
- 3.8 solidity資料型別詳解Solid資料型別