Struts2 ResultType筆記

 一個result代表了一個可能的輸出。當Action類的方法執行完成時,它返回一個字符串類型的結果碼,框架根據這個結果碼選擇對應的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錯誤

Struts2的resultType種類

a.struts2自帶的類型實現

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

b. 一些插件支持的類型,ex:struts2-json-plugin

Struts2的resultType詳解

1. dispatcher:用來轉向頁面,通常處理 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靜態常量,專門用於指定默認的參數名。故上面例子可以簡化成:

  <result name="success">/success.jsp</result>

        B、如果是帶參數的,可以使用OGNL表達式

  <result name=“success” >viewNews.jsp?id=${id}</result>

2、redirect結果類型(重定向到一個Url,也可以是Action或一個頁面)
Redirect結果類型在後臺使用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL,它的實現類是org.apache.struts2.dispatcher.ServletRedirectResult.該類同樣有二個屬性(property):location和parse,在使用redirect結果類型的場景中,用戶要完成一次與服務器之間的交互,瀏覽器需要完成兩次請求,因此第一次請求中的數據在第二次請求中是不可用的,這意味在目標資源中是不能訪問action實例、action錯誤以及錯誤等。
如果有某些數據需要在目標資源中訪問,
  i、一種方式是將數據保存到Session中,
  ii、另一種方式是通過請求參數來傳遞數據。

  示例 

<result name="success" type="redirect">/foo.jsp?id=${id}</result>
<result name="topic" type="redirect">/findTopics.actiono?topicId=${topicId}</result> 

3、redirectAction結果類型(重定向到一個Action)

  他經常用於防止表單重複提交,比方說在增加完用戶之後要顯示列表。redirectAction結果類型的實現類是org.apache.struts2.dispatcher.ServletActionRedirectResult,該類是ServletRedirectResult的子類,因此我們也就可以判斷出redirectAction結果類型和redirect結果類型的後臺工作原理是一樣的,即都是利用HttpServletResponse的sendRedirect方法將請求重定向到指定的URL。action處理完後重定向到一個action,請求參數全部丟失,action處理結果也全部丟失。

<result name="topic" type="redirectAction">   
<param name="actionName">findTopics</param>
<param name="namespace">/</param>   
<param name="topicId">${topicId}</param>   
</result>  

4、chain結果類型(action鏈)

  a. 使用chian的方式,後面的Action會和前面的Action共用同一個ActionContext
  b. 名稱爲“chain”的ResultType在配置的時候,除了前面示例中的actionName外,還有一個參數,名稱爲“namespace”,表示被鏈接的Action所在包的命名空間,如果不設置,默認的即是當前的命名空間。

  <result name="runTimeEx" type="chain"> 
          <param name="actionName">errorProcessor</param>  
  <param name="namespace">/</param>

PS:chain有一個坑需要注意的是action1傳給action2時除了攔截器chain起作用外,表單處理攔截器param也會起作用,而且param在chain之後起作用,故如果param和chain都給同一個參數賦值的話,param會覆蓋chain的效果,例如表單提交了一個c, action1裏對c進行了處理, 處理結果還是保留在c上,接下來你想把處理後的c傳給action2處理,action2也用c來接收,這時候就會出現action2中的c的值是表單提交過來的值而不是action1傳過來的值,給你一種參數沒傳遞的錯覺,其實是被覆蓋了,一般我的做法是action1中提供getB方法返回c的值,action2中不要提供setC而是提供setB來給c賦值。參考:https://blog.csdn.net/randomnet/article/details/8656759

其他幾種就先不寫啦,後續如有需要再補充了~~

最後說一句,不要在瀏覽器裏面直接傳遞中文參數!!! 不要在瀏覽器裏面直接傳遞中文參數!!! 不要在瀏覽器裏面直接傳遞中文參數!!! 

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