File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes ActionErrors and redirect = true Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ActionErrors and redirect = true" Watch "ActionErrors and redirect = true" New topic

ActionErrors and redirect = true

Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83
Hi Everyone,

I am having troubles :-(. I have followed the PRG (post, redirect, get) pattern for my application which works wonderfully. My problem is that the actionerrors that are saved in my action (with saveerrors) are lost when the redirect executes. I understand why this happens (because they are stored in the request, not the session). What I need to know is, what is the best way to work around this problem? I have read a few different ways to work this out such as:
- subclassing Action and overriding the saveErrors method like this:

- saving the errors in the session then reading the from the session in the validate method of the error form that I redirect to.

I'm sure this isnt a new problem and wondering what is the best practice out there and could really use some help.

Thanks guru's.


There are only 10 types of people in this world... Those who understand binary and those who don't.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I would suggest that you not override the saveErrors method of the Action class, but just use it as-is. This method is overloaded with two different signatures: one that accepts a session as a paramaeter and one that accepts a request. Just use the one that accepts a session as a parameter, and your messages will be saved in the session, thereby making them available even after a redirect.

Consultant, Sima Solutions
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
One of the thing you need to take care is keeping the session clean. From the code you posted I couldn't make out if you are binding just one exception or more than that. If you actually make use of ActionErrors or ActionMessages then you must clean the error messages session more often (in fact after every request) than overwriting only if it exists.

It's critical as for as I see as in the application I am working, on specific circumstances exception surfaced in wrong pages.
I agree. Here's the link:
subject: ActionErrors and redirect = true
It's not a secret anymore!