1

Is there any sample code where I can see the use of scope interceptor in Struts2? I want to pass a parameter from one action to other action (configured through struts.xml) & want to use scope interceptor.

Since I'm new to Struts 2, can any one provide sample of using scope interceptor?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Mike
  • 7,606
  • 25
  • 65
  • 82

1 Answers1

2

I believe this is very well described in the Struts2 documentation.hers is all you have to do

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

All you need to take care is that, you have a getter in ActionA and and a similar setter in actionB. Also, you should use a key parameter to make sure you tell Struts2 which action gets which objects

read this official documentation for detail Struts2 Scope Interceptor

I will prefer Scope Interceptor only when i have to develop a wizard like functionality as it will handle other things like session-level locking. If this is not your requirement there are other way to pass parameters like putting object in Session and getting object from session at second action

beendr
  • 654
  • 7
  • 21
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204