Struts2 Lifecycle of Action object
struts2
Solution
- 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.
Problem
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.