I'm working my way through the book Struts 2 in Action, but am wondering what is the correct way to implement the Post-Redirect-Get design pattern in Struts 2 (I'm using Struts 184.108.40.206).
What I'm wondering about is how to maintain the model data. In Struts 2, the data is actually on the action object itself, but the PRG design uses two actions - the Task Controller and the Page Controller.
I would like to keep my model data in one place only, so I looked into having my actions implement ModelDriven. However, I'm confused about where the model object will be stored. All the examples I've seen show the action implementation creates the model object either when the action is instantiated, or when the getModel() method is called. Of course, if I create the model object in my Page Controller, I'll lose any data I collected in my Task Controller.
Assuming I don't want to keep it in the database (at least not yet), do I need to store it in the session (using get/setAttribute)? I'm concerned this is the wrong approach because I've read that I shouldn't need to access the session directly when using Struts 2. Perhaps this is an exception case?
My question is: Do I need to maintain my model object in the session (or wherever) and in the getModel() method of both actions, create a new model instance only if I can't find one already in the session?
I think then my Task Controller will need to pull this object from the session, then place it back in the session just before redirecting to the Page Controller. The Page Controller will need to pull it from the session, but won't need to put it back before it forwards to the JSP page.
Thanks for the tip. I've been searching through documentation, the Struts 2 in Action book and general web searches, and can't quite find all the pieces I need to put this together. Can you please look at what I've done and suggest what I'm still missing? A pointer to a tutorial on implementing this feature would be great.
First, I've created my two actions, named TCCreateMemberAction (the Task Controller) and CreateMemberAction (the Page Controller) such that they implement the ScopedModelDriven interface, as in: Both actions are declared in a package which extends the struts-default package
With each of these action classes, I've implemented the methods declared by the interface, but for now, they don't do much (I'm waiting to see what they do so I can decide what to implement there).
I then added the <interceptor-ref> elements to the declarations for my actions in the struts.xml file. Following is the action element for one of these:
However, when the actions are executed, the ScopedModelDriven methods are never called. I see the framework calls getModel() (twice) and then execute().
Can you see what I'm missing?
Below is my complete struts2.xml file, just in case you need to see it:
Mark E Hansen
Joined: Apr 01, 2009
Well, I got it working. I had a couple typographical errors in the parameter name values passed to the interceptor. I used scopedModelDrive.scope rather than scopedModelDriven.scope.
I do have a couple other questions about how this works, but as I've resolved the issue with the topic, I'll mark it resolved and open a new one for my questions.