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 2.1.8.1).
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.