struts2 result param

   

在struts.xml配置文件中遇到了param,有點迷糊,上網查了查……似乎明白些了

<action name="Log*" method="loginFrame{1}" class="member.loginIndeAct">
   <result>${tplPath}</result>
   <result name="logout" type="redirectAction">
        <param name="namespace">/jeecms</param>
        <param name="actionName">LoginFrameInput</param>
   </result>
  </action>

這裏的action   是Log,他包含了登陸和退出,當系統在登錄的時候就直接返回${tplPath}就可以了;在退出的時候需要返回到登陸頁面,所以退出的時候是轉發到 action的,轉發到action肯定要告訴系統轉發到那個action,該action的namespace是什麼,名字是什麼,就是通過param 該屬性老告知struts2的;當然param還有其他用法,不過一般就用這個就差不多了,綜上得出:

param標籤主要用於爲其他標籤提供參數,例如bean和include標籤。
param參數設置:
name:可選屬性,指定設置參數名稱
value:可選屬性,指定參數的值
id:可選屬性,指定該元素引用id

看到這,野豬似乎感覺清楚了許多,嘿嘿……

下面還搜到了一些信息,等野豬不是很清楚的時候再看看吧……

chain      
    用來處理Action鏈,被跳轉的action中仍能獲取上個頁面的值,如request信息。      
    com.opensymphony.xwork2.ActionChainResult      
dispatcher      
    用來轉向頁面,通常處理JSP      
    org.apache.struts2.dispatcher.ServletDispatcherResult      
freemaker
      
    處理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      
plainText      
    顯示原始文件內容,例如文件源代碼      
    org.apache.struts2.dispatcher.PlainTextResult      
plaintext      
    顯示原始文件內容,例如文件源代碼      
    org.apache.struts2.dispatcher.PlainTextResult 
redirect    
    重定向到一個URL ,被跳轉的頁面中丟失傳遞的信息,如request     
    org.apache.struts2.dispatcher.ServletRedirectResult      
redirectAction
      
    重定向到一個Action ,跳轉的頁面中丟失傳遞的信息,如request        
    org.apache.struts2.dispatcher.ServletActionRedirectResult      
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 傳遞參數

 

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

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

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

  4.   </result>  

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

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

  7.  </result>  

  8. </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>
view plaincopy to clipboardprint?private boolean success  = true;  private String result = "main.action";  //getter和setter方法略
以上的success和result互相對應到了
view plaincopy to clipboardprint?<param name="includeProperties">success,result</param>
struts2會根據其設置的值匹配跳轉
對於json一般情況下很少用到,但是在處理ext的時候會用到這個屬性類型,這個地方也是經常被忽略的。

最後感覺還是應該查一下文檔給野豬看看,嘿嘿:

Redirect Action Result

  • actionName (default) - the name of the action that will be redirect to

  • namespace - used to determine which namespace the action is in that we're redirecting to . If namespace is null, this defaults to the current namespace

<package name="public" extends="struts-default">
    <action name="login" class="...">
        <!-- Redirect to another namespace -->
        <result type="redirect-action">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action></package><package name="secure" extends="struts-default" namespace="/secure">
    <-- Redirect to an action in the same namespace -->
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
        <result name="error" type="redirect-action">error</result>
    </action>

    <action name="error" class="...">
        <result>error.jsp</result>
    </action></package><package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <-- Pass parameters (reportType, width and height) -->
   <!--
   The redirect-action url generated will be :
   /genReport/generateReport.action?reportType=pie&width=100&height=100
   -->   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirect-action">
         <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>
      </result>
   </action></package>

Redirect Result

  • location (default) - the location to go to after execution.

  • parse - true by default. If set to false, the location param will not be parsed for Ognl expressions.

  • <result name="success" type="redirect">
      <param name="location">foo.jsp</param>
      <param name="parse">false</param></result>

       

    <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
       <-- Pass parameters (reportType, width and height) -->
       <!--
       The redirect-action url generated will be :
       /genReport/generateReport.jsp?reportType=pie&width=100&height=100
       -->
       <action name="gatherReportInfo" class="...">
          <result name="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>

       

這下感覺怎麼樣了?野豬!多看看文檔!

最後,又看了一眼result的默認參數:

<result name="success" type="dispatcher">
    <param name="location">/ThankYou.jsp</param></result>

呵呵……這還比較滿意!吃飯去!野豬!

   


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