2009-07-01 24 views
10

如果我有一個動作,其結果是對另一個類中的另一個動作的redirectAction,是否可以獲取驗證錯誤以顯示在結果操作中?例如。在下面的例子中,如果用戶執行actionA(沒有與它相關的視圖),並且有錯誤,是否有任何方法在actionB結果(foo.jsp)中顯示這些錯誤?還是我完全用錯誤的方式去解決這個問題?我可以在不同的動作類之間傳播struts2 ActionErrors嗎?

<package name="a" extends="struts-default" namespace="/a"> 
    <action name="actionA" class="actionAClass"> 
     <result name="input" type="redirectAction"> 
      <param name="actionName">actionB</param> 
      <param name="namespace">/b</param> 
     </result> 
     <result type="redirectAction"> 
      <param name="actionName">actionB</param> 
      <param name="namespace">/b</param> 
     </result> 
    </action> 
</package> 
<package name="b" extends="struts-default" namespace="/b"> 
    <action name="actionB" class="actionBClass"> 
     <result>/foo.jsp</result> 
    </action> 
</package> 
+0

嘗試使用MessageStore攔截 - http://struts.apache.org/release/2.3.x/docs/message-store-interceptor.html – 2013-04-17 01:31:35

回答

4

可能有辦法做到這一點,但我不認爲這是一個使用struts的好方法。如果actionA未通過驗證,那麼您很可能會希望爲其顯示錯誤的非重定向輸入結果,或者可能會顯示一個全局錯誤頁面。

我想你可以在重定向之間的某個地方存儲動作錯誤,但是你不會真正在使用框架的設計。

0

缺省情況下,Struts2不支持此功能。儘管存在解決方案(它是通過在會話中存儲消息的簡單的struts攔截器來完成的)。

solution with source code

7

基本上,你必須使用預定義的攔截器叫店這需要一個operationMode:存儲和檢索:

<package name="a" extends="struts-default" namespace="/a"> 
    <action name="actionA" class="actionAClass"> 
     <!-- Here you are storing the Error messages --> 
     <interceptor-ref name="store"> 
      <param name="operationMode">STORE</param> 
     </interceptor-ref> 

     <!-- include your default stack in case you need to load other interceptors --> 
     <interceptor-ref name="defaultStack" /> 

     <result name="input" type="redirectAction"> 
      <param name="actionName">actionB</param> 
      <param name="namespace">/b</param> 
     </result> 
     <result type="redirectAction"> 
      <param name="actionName">actionB</param> 
      <param name="namespace">/b</param> 
     </result> 
    </action> 
</package> 
<package name="b" extends="struts-default" namespace="/b"> 
    <action name="actionB" class="actionBClass"> 

     <interceptor-ref name="store"> 
      <param name="operationMode">RETRIEVE</param> 
     </interceptor-ref> 

     <!-- include your default stack in case you need to load other interceptors --> 
     <interceptor-ref name="defaultStack" /> 

     <result>/foo.jsp</result> 
    </action> 
</package> 
10

Struts2的默認有一個商店攔截。它以STORE模式在會話中存儲actionMessages,actionErrors和fieldErrors,並且可以通過在RETRIEVE模式下使用相同的攔截器在下一次重定向中檢索相同的重定向。更多的細節可以在第一個動作中找到here

2

使用ActionContext.getContext().getSession().put(key, value)並使用ActionContext.getContext().getSession().get(key)redirectedActionaddActionErrors的主要行動

+0

我想MessageStoreInterceptor會是一個更好的方法。 – 2013-04-17 01:21:50

+0

MessageStoreInterceptor - http://struts.apache.org/release/2.3.x/docs/message-store-interceptor.html – 2013-04-17 01:31:05

0

您可以使用結果類型「鏈」檢索。

<action name="delete" class="com.example.Delete"> 
    <result name="error" type="chain"> 
     <param name="actionName">show</param> 
    </result>   
</action> 
<action name="show" class="com.example.Show"> 
    <result name="success" type="dispatcher">/jsp/show.jsp</result>      
</action> 

在show.jsp可以顯示你在刪除操作

+0

不鼓勵使用`chain`。 – 2016-01-27 10:12:44

6

設置我找到一個更好的解決方案,以傳遞actionRedirect結果類型採取行動的錯誤和消息的錯誤動作或動作的消息。它爲我工作。

<action name="action1" class="action.Action1" > 
    <result>/abc.jsp</result> 
    <result name="input" type="redirectAction"> 
    <param name="actionName">action2</param> 
    <param name="param1">${param1}</param> 
    <param name="param2">${param2}</param> 
    <param name="actionErrors">${actionErrors}</param> 
    </result> 
    </action> 
    <action name="action2" class="action.Action2" > 
    <result>/def.jsp</result> 
    <result name="input">/def.jsp</result> 
    </action/> 

這是它.....快樂編碼

3

結果類型鏈將消息/錯誤複製到所產生的動作,如果你在struts.xml中或struts.properties文件如下 -

struts.xwork.chaining.copyErrors - set to true to copy Action Errors 
struts.xwork.chaining.copyFieldErrors - set to true to copy Field Errors 
struts.xwork.chaining.copyMessages - set to true to copy Action Messages 

例(在struts.xml) -

<constant name="struts.xwork.chaining.copyErrors" value="true"/> 
0

這項工作在我

在struts中添加此行。XML:

<constant name="struts.xwork.chaining.copyErrors" value="true"/> 
<constant name="struts.xwork.chaining.copyMessages" value="true"/> 

使用結果類型 「鏈」,並以名稱 「輸入」 添加結果:

<package name="a" extends="struts-default" namespace="/a"> 
    <action name="actionA" class="actionAClass"> 
     <result name="input" type="chain"> 
      <param name="actionName">actionB</param> 
      <param name="namespace">/b</param> 
     </result> 
     <result type="chain"> 
      <param name="actionName">actionB</param> 
      <param name="namespace">/b</param> 
     </result> 
    </action> 
</package> 
<package name="b" extends="struts-default" namespace="/b"> 
    <action name="actionB" class="actionBClass"> 
     <result>/foo.jsp</result> 
     <result name="input">/foo.jsp</result> 
    </action> 
</package> 
1

商店攔截器(MessageStoreInterceptor)可用於存儲和檢索actionErrorsactionMessagesfieldErrors

您可以通過operationMode參數傳遞給操作動態更改商店攔截器的操作

http://localhost/sample.action?operationMode=STORE

您可以在STORE模式店裏攔截器在默認堆棧,它使所有的行動消息存儲在會話中。

<interceptor-ref name="store"> 
      <param name="operationMode">STORE</param> 
    </interceptor-ref> 

得到你需要在RETRIEVE模式中添加store攔截到需要這些信息的具體行動的消息。

這是被重定向到一個樣品全局錯誤頁面,這個動作可以閱讀actionErrorsfieldErrorsactionMessages當我們添加的store攔截它,並設置operationModeRETRIEVE

@Action(value = "error-page" , 
       interceptorRefs = 
        {@InterceptorRef(value = "store", params = {"operationMode", "RETRIEVE"})} 
      ) 
public String execute() throws Exception {  
    LOG.error("An error accrued during action ActionErrors '{}' , FieldErrors '{}' " , getActionErrors() , getFieldErrors()); 
    //Rest of the code 
} 

MessageStoreInterceptor刪除之前的錯誤添加新的。

您可以將AUTOMATIC中的商店設置爲默認堆棧。通過這種方式,所有消息總是被存儲並且當動作結果爲類型ServletRedirectResult時(例如,如果動作'redirectAction','重定向'),將自動重試。因此,您不需要在RETRIEVE模式中定義明確的store攔截器。

雖然這不是一個好方法,但您可以通過這些密鑰訪問會話中的商店消息。

session.get(MessageStoreInterceptor.fieldErrorsSessionKey) 
session.get(MessageStoreInterceptor.actionErrorsSessionKey) 
session.get(MessageStoreInterceptor.actionMessagesSessionKey) 
相關問題