If I have an action where the result is a redirectAction to another action in a different class, is it possible to get validation errors to display in the resulting action? E.g. in the following example, if a user executes actionA (which has no views associated with it), and there are errors, is there any way to display those errors in the actionB result (foo.jsp)? Or am I going about this in completely the wrong way?
<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>
There may be a way to do that, but I don't think it's a very good way to use struts. If actionA is failing validation, you most likely would want to either have a non-redirect input result for it that shows the errors, or perhaps a global error page that can show it.
I suppose you could store the action errors somewhere like the session in between the redirect, but you wouldn't really be using the framework how it was designed.
Struts2 by default has a store interceptor. It stores the actionMessages, actionErrors and fieldErrors in session in STORE mode and you can retrieve the same in the next redirect by using the same interceptor by using it in RETRIEVE mode. More details can be found here
This functionality is not supported by Struts2 by default. Solution exists though (it is done by simple struts interceptor that stores messages in session).
solution with source code
The store interceptor (
MessageStoreInterceptor
) can be used to store and retrieveactionErrors
,actionMessages
andfieldErrors
.You can change the operation of store interceptor on the fly by passing the
operationMode
parameter to actionhttp://localhost/sample.action?operationMode=STORE
You can set the store interceptor in
STORE
mode in your default stack which enables all action message to be stored in the session.To get the messages you need to add
store
interceptor inRETRIEVE
mode to the specific action which needs these messages.This is a sample global error page which is redirected to, this action can read
actionErrors
,fieldErrors
andactionMessages
when we add thestore
interceptor to it and set theoperationMode
toRETRIEVE
:The
MessageStoreInterceptor
remove previous errors before adding new ones.You can set the store in
AUTOMATIC
in you default stack. In this way all messages are stored always and will automatically retried when the action result is type ofServletRedirectResult
(For example if the action 'redirectAction', 'redirect') So you don't need to define an explicitstore
interceptor inRETRIEVE
mode.Although it is not a good approach but you can access the store messages in the session with these keys.
You can use result type "chain".
in show.jsp you can display action errors or action messages that you set in delete action
I find a better solution to pass action errors and messages on actionRedirect result type. It is working for me.
This is it ..... Happy coding