2011-12-09 66 views
1

有沒有任何示例代碼,我可以看到在Struts2中使用範圍攔截器?我想將一個參數從一個動作傳遞給其他動作(通過struts.xml配置)&想要使用範圍攔截器。在struts2中的範圍攔截器

由於我是Struts 2的新手,任何人都可以提供使用範圍攔截器的示例嗎?

回答

2

我相信這是Struts2的documentation.hers很好的描述是所有你需要做的

<action name="scopea" class="ScopeActionA"> 
       <result name="success" type="dispatcher">/jsp/test.jsp</result> 
       <interceptor-ref name="basicStack"/> 
       <interceptor-ref name="scope"> 
        <param name="key">funky</param> 
        <param name="session">person</param> 
        <param name="autoCreateSession">true</param> 
       </interceptor-ref> 
      </action> 

<action name="scopeb" class="com.mevipro.test.action.ScopeActionB"> 
      <result name="success" type="dispatcher">/jsp/test.jsp</result> 
      <interceptor-ref name="scope"> 
       <param name="key">funky</param> 
       <param name="session">person</param> 
       <param name="autoCreateSession">true</param> 
      </interceptor-ref> 
      <interceptor-ref name="basicStack"/> 
     </action> 

所有你需要照顧的是,你有ActionA和getter和類似的二傳手在actionB中。此外,你應該使用一個關鍵參數,以確保你告訴Struts2的哪個動作獲得哪些對象

閱讀詳細介紹這個官方文檔 Struts2 Scope Interceptor

我會更喜歡範圍攔截,只有當我有開發類似功能的嚮導因爲它會處理其他事情,如會話級鎖定。 如果這不是你的要求,還有其他方式來傳遞參數,比如將對象放入Session中,並在第二個動作中從會話中獲取對象