views:

1418

answers:

3

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>
A: 

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.

Brian Yarger
A: 

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

rdk
A: 

Basically you have to use predefined interceptors called store which takes operationMode: store and retrieve:

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