We are using struts 1.2.2 and have a situation here.
So we have 2 forms FormA and FormB.
We also have 2 actions ActionA and ActionB.
Now ActionA when invoked it gets FormA as the submitted bean. It does all processing, saves the form , creates an OrderId and if successful it forwards to ActionB. ActionB then populates FormB based on the OrderId and then forwards to the next jsp.
We don’t want the 2 actions to share a form as the contents are very different and we want the forms to only represent what it represents on the jsp page. Also on every page we are saving the content on the Db. The flow has 5 pages and one can leave after 2 pages – come back again and start from page 1. But page 1 and page 2 should retrieve the data from the DB and display. So if an orderID exists we populate the form based on that orderId by fetching data from the DB.
However I don’t find an easy way of passing this orderId from one action to another.
I can’t use request.setAttribute() at the end of ActionA as by the time the control reaches ActionB it now has a completely new Request Object. I am guessing on the forward that happens from one action to another the request gets refreshed. However if it forwards to a jsp the request is retained.
Don’t want to store the orderId in session as session.setAttribute() as someone may attempt a bookmarked URL and then that page would simply pick up the last stored OrderId in session and try to process that.
Also no way we can put that in formbeans as that is not shared.
One option is to pass that in the URL as a param e.g. ActionB?OrderId=89 . However downside to that is someone can change the orderId in the URL and try to fetch data for that random orderId. While we can always put in checks to prevent someone not authorized to view a order that belongs to another customer it is a convoluted process at the moment. Also in Struts 1.2.2 I think I will have to create a new ActionForward object programmatically and then append “?OrderId=89” explicitly at the end of the current path.
Any help friends?
Actually, I would put it in the session. Then when you retrieve it from ActionB, just delete it immediately.
This is typically known as a Flash scoped variable. There is no official Flash scope in Struts, but this simulates it.
Basically, the fear of reloading the OrderId is gone here because ActionA will set this variable in the session, forward to ActionB and ActionB will get it out of the session and delete it. The only way someone could have that stuck in their session is if they stop the request after ActionA is done but before ActionB runs, which is highly unlikely and would just get removed once they view ActionB anyway, which probably wouldn’t be that bad.
This also let’s you put in ActionB to check to make sure an OrderID is in the session and, if not, forward back to ActionA.