struts2 result param

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

<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>java

這裏的action 是Log,他包含了登錄和退出,當系統在登陸的時候就直接返回${tplPath}就能夠了;在退出的時候須要返回到登錄頁面,因此退出的時候是轉發到 action的,轉發到action確定要告訴系統轉發到那個action,該action的namespace是什麼,名字是什麼,就是經過param 該屬性老告知struts2的;固然param還有其餘用法,不過通常就用這個就差很少了,綜上得出:express

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

看到這,野豬彷佛感受清楚了許多,嘿嘿……json

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

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區別jsp

1、使用redirect須要後綴名 使用redirect-action不須要後綴名
2、type="redirect" 的值能夠轉到其它命名空間下的action,而redirect-action只能轉到同一命名空下的 action,所以它能夠省略.action的後綴直接寫action的名稱。
如:ide

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

附:redirect-action 傳遞參數 url

 
  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中很是有用)
示例
  
  
           
  
  
  1. <package name="struts2" extends="json-default" namespace="/">  
  2.         <action name="login" class="loginAction" method="login">  
  3.             <result type="json">  
  4.                 <param name="includeProperties">success,result</param>                
  5.             </result>               
  6.         </action>  
  7.         <action name="main" class="loginAction" method="main">  
  8.             <result name="main">/index.jsp</result>           
  9.         </action>   
  10.     </package>  
  
  
           
  
  
  1. private boolean success  = true;  
  2. private String result = "main.action";  
  3. //getter和setter方法略  
以上的success和result互相對應到了
  
  
           
  
  
  1. <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>

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

相關文章
相關標籤/搜索