天天看點

Struts2 result中type的參數說明

1:chain   

    用來處理Action鍊,被跳轉的action中仍能擷取上個頁面的值,如request資訊。   

    com.opensymphony.xwork2.ActionChainResult    

2:dispatcher   

    用來轉向頁面,通常處理JSP   

    org.apache.struts2.dispatcher.ServletDispatcherResult    

3:freemaker   

    處理FreeMarker模闆   

    org.apache.struts2.views.freemarker.FreemarkerResult    

4:httpheader   

    控制特殊HTTP行為的結果類型   

    org.apache.struts2.dispatcher.HttpHeaderResult    

5:stream   

    向浏覽器發送InputSream對象,通常用來處理檔案下載下傳,還可用于傳回AJAX資料   

    org.apache.struts2.dispatcher.StreamResult    

6:velocity   

    處理Velocity模闆   

    org.apache.struts2.dispatcher.VelocityResult   

7:xslt   

    處理XML/XLST模闆   

    org.apache.struts2.views.xslt.XSLTResult    

8:plainText   

    顯示原始檔案内容,例如檔案源代碼   

    org.apache.struts2.dispatcher.PlainTextResult    

9:plaintext   

    顯示原始檔案内容,例如檔案源代碼   

    org.apache.struts2.dispatcher.PlainTextResult 

10:redirect   

    重定向到一個URL ,被跳轉的頁面中丢失傳遞的資訊,如request  

    org.apache.struts2.dispatcher.ServletRedirectResult    

11:redirectAction   

    重定向到一個Action ,跳轉的頁面中丢失傳遞的資訊,如request     

    org.apache.struts2.dispatcher.ServletActionRedirectResult    

12:redirect-action   

    重定向到一個Action ,跳轉的頁面中丢失傳遞的資訊,如request     

    org.apache.struts2.dispatcher.ServletActionRedirectResult

注:redirect與redirect-action差別

一、使用redirect需要字尾名 使用redirect-action不需要字尾名

二、type="redirect" 的值可以轉到其它命名空間下的action,而redirect-action隻能轉到同一命名空下的 action,是以它可以省略.action的字尾直接寫action的名稱。

如:

<result name="success" type="redirect">viewTask.action</result>

<result name="success" type="redirect-action">viewTask</result>

附:redirect-action 傳遞參數

<action name="enterpreinfo" class="preinfoBusinessAction"    method="enterPreinfoSub">  

  <result name="success" type="redirect-action">  

     showpreinfo?preinfo.order_number=${preinfo.order_number}&amp;preinfo.company_name=${preinfo.company_name}   

  </result>  

<result name="error" type="redirect">  

    <param name="location">/error.jsp</param>  

</result>  

</action>  

   因為使用了redirect-action,是以要注意不能将 showpreinf?preinfo.order_number=${preinfo.order_number}寫成 showpreinf.action?preinfo.order_number=${preinfo.order_number}

其中${}為EL表達式,擷取action:enterpreinfo中屬性的值;在這個配置檔案裡,多個參數的連接配接符使用了"&amp;",但XML的文法規範,應該使用"&amp;"代替"&",原理和HTML中的轉義相同,開始沒有注意,在struts分析配置檔案時,總是報出這樣的錯誤:

json   一般很容易忽略的一個地方(在EXT中非常有用)示例view plaincopy to clipboardprint?<package name="struts2" extends="json-default" namespace="/">           <action name="login" class="loginAction" method="login">               <result type="json">                   <param name="includeProperties">success,result</param>                             </result>                        </action>           <action name="main" class="loginAction" method="main">               <result name="main">/index.jsp</result>                    </action>        </package>  <package name="struts2" extends="json-default" namespace="/">

  <action name="login" class="loginAction" method="login">

   <result type="json">

    <param name="includeProperties">success,result</param>   

   </result>   

  </action>

  <action name="main" class="loginAction" method="main">

   <result name="main">/index.jsp</result>  

  </action>

</package> view plaincopy to clipboardprint?private boolean success  = true;   private String result = "main.action";   //getter和setter方法略  private boolean success  = true;

private String result = "main.action";

//getter和setter方法略

  以上的success和result互相對應到了view plaincopy to clipboardprint?<param name="includeProperties">success,result</param>    <param name="includeProperties">success,result</param>  struts2會根據其設定的值比對跳轉對于json一般情況下很少用到,但是在處理ext的時候會用到這個屬性類型,這個地方也是經常被忽略的。

繼續閱讀