Scope interceptor in struts2 - struts2

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?

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

Related

Passing javabean as parameter in struts.xml

<action name="someAction" class="">
<result name="showDetails" type="redirectAction">
<param name="actionName">someAction</param>
<param name="method">someMethod</param>
<param name="javabean.fieldOne">${javabean.fieldOne}</param>
<param name="javabean.fieldTwo">${javabean.fieldTwo}</param>
...
...
</result>
</action>
So instead of explicitly listing all the fields that I want to pass to the method, is it possible to pass the whole bean as parameter to the next action?
Note: I've tried ${javabean}, but it gave me some class reference number as a string literal in an object. AFAIK, there's no way to recreate the object with this reference id.

Passing parameters in URL query string to Action through interceptor

I have an interceptor that is working fine except the query string is not being passed on to the action from the interceptor.
<action name="EditIroCase" class="iro.action.IroCaseAction">
<interceptor-ref name="authIro"/>
<result name="success">iro/IroCaseFORM.jsp</result>
</action>
URL: /EditIroCase.action?id=123
Action is an existing one that I am adding the interceptor to. Implementing ParametersAware in Action to get ID from URL and that works fine.
Tried a couple different things, but can't seem to make it work. Used lots of interceptors, just never needed to maintain a param across its execution.
Interceptors work slightly different rather than actions. Because they belong to the action. So you can't add an action to the interceptor, but interceptor to the action.
You should know if you are using xml configuration or annotations, or another configuration provider, interceptors are stored in the collection such as List or Set, doesn't matter, but all of them in one place. Using <interceptor-ref> tag inside the <action> overrides the interceptor's config. By default actions are configured using <default-interceptor-ref> tag, which is defined in the struts-default.xml from the core package, and it points to defaultStack. This stack is designed to serve all your needs, but if you use a custom interceptor then you should explicitly add reference to defaultStack or create a custom stack which you would reference.
<action name="EditIroCase" class="iro.action.IroCaseAction">
<interceptor-ref name="authIro"/>
<interceptor-ref name="defaultStack"/>
<result name="success">iro/IroCaseFORM.jsp</result>
</action>
Now parameters should be populated.

about the struts2 redirectAction result in struts.xml

Can Struts2 redirectAction result transfer a reference type like Person.
my code
<action name="saveAdAction" class="saveAdAction">
<result name="success" type="redirectAction">
<param name="actionName">getAdAction</param>
<param name="namespace">/</param>
<param name="person">${person}</param>
</result>
</action>
<action name="getAdAction" class="getAdAction">
<result name="success">/ad/ad.jsp</result>
<result name="input">/index.jsp</result>
</action>
the person is an bean in saveAdAction and getAdAction.
when i use saveAdAction the webpage will jump into index.jsp. why?
No, a redirect can only transfer request parameters, just like any regular servlet redirect. So you either have to pass each attribute of person as request parameters, or put the person in the session with a key that you can look up in your next action.
No, This is not possible, with redirectAction a new value stack will be created and new instance of Request and Response will be created.
So all your previous data will be lost.There are few ways by which you can transfer data and here are few
Save data in session and retrieve it in other action.
Pass through Query string which is not possible in your case.
Use message-store-interceptor
You can choose any of the above option

Struts2 Lifecycle of Action object

I'm newbie to Struts2 framework and i didn't really have much time to read any Struts2 books. I just learn it from YouTube. Anyway here's my question.
Supposedly my struts.xml is as per below:
<struts>
<package name="p1" namespace="/" extends="struts-default">
<action name="login" class="org.tutorial.struts2.action.LoginAction">
<result name="success" type="redirect" >searchTutorialForm</result>
<result name="error">/login.jsp</result>
</action>
<action name="searchTutorialForm">
<result>/searchForm.jsp</result>
</action>
</package>
</struts>
Lets talk about package p1.
If the URL is say [http://localhost:8080/Struts2/login], the org.tutorial.struts2.action.LoginAction gets called and upon success it gets redirected to the searchTutorialForm action tag which calls the searchForm.jsp.
So the URL the client would see is [http://localhost:8080/Struts2/searchTutorialForm]
(purpose is not for the client to see [http://localhost:8080/Struts2/searchForm.jsp])
Now there are some member variables in LoginAction which is being displayed in searchForm.jsp using tag. However using this approach they are not displayed since i think the object LoginAction is no longer in the ValueStack (after the redirect, i think??).
Of course if i do not use the above approach but instead as per below:
<struts>
<package name="p1" namespace="/" extends="struts-default">
<action name="login" class="org.tutorial.struts2.action.LoginAction">
<result name="success">/searchForm.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
then the member variable in the LoginAction object is displayed in success.jsp using the tag (but then user would see the URL [http://localhost:8080/Struts2/searchForm.jsp])
Basically my intention is for the user not to see any specific internal file or calling like .jsp or .action.
IMPORTANT NOTE:
There is no action class in action tag searchTutorialForm - basically a dummy action.
Question:
1. How do i display the member variable in LoginAction object using the first approach?
2. What is the life cycle of the LoginAction object in Value Stack?
Thanks.
Actions are created per-request.
A redirect causes a new request.
Therefor objects in previous actions are no longer available.
The questions:
You put it in session or include them as parameters in the redirect.
Actions go away at the end of the request.

Struts2 : How to use an attribute of an action class in another action class?

I have 2 Action classes : LoadingFormAction.java and FormValidationAction.java. In the first one, I create the functions which help in uploading data from an Excel file, and in the second I need to validate the loaded data according to the other fields values. LoadingFormAction.java control the view Form.jsp and FormValidationAction.java the view FormValidation.jsp. My question is : How to evaluate the attributes of the first action and use them in the second Action?? Is it obligatory to pass it as a session parameter? Thank you a lot.
<action name="LoadingFormActionName" class="com.test.LoadingFormAction">
<result name="success" type="redirectAction">
<param name="actionName">FormValidationActionName</param>
<param name="yourProperty">${yourProperty}</param>
</result>
</action>
Where yourProperty is the parameter of your LoadingFormAction class which you want to pass
to FormValidationAction class. You need to declare getter/setter for this variable in both the classes. Also, LoadingFormActionNameand FormValidationActionName are the corresponding action names.

Resources