File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Post-Redirect-Get pattern with Struts 2 ModelDriven action? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Post-Redirect-Get pattern with Struts 2 ModelDriven action?" Watch "Post-Redirect-Get pattern with Struts 2 ModelDriven action?" New topic
Author

Post-Redirect-Get pattern with Struts 2 ModelDriven action?

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
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.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Check out the "scopedModelDriven" interceptor.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
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
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
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.

Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Post-Redirect-Get pattern with Struts 2 ModelDriven action?
 
Similar Threads
Difference between servlet and struts
Accessing a variable from both EL and JSP Expression?
how to implement pojo to web form mapping in struts2
Session invalidation - getModel called from JSP?
Some questions about the Scoped Model Driven interface of Struts 2