aspose file tools
The moose likes Struts and the fly likes How to return to the last page in failure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to return to the last page in failure" Watch "How to return to the last page in failure" New topic

How to return to the last page in failure

jon lai

Joined: Jun 09, 2006
Posts: 1

The application I am developing is Struts based and consists of nested jsps and tiles.

Suppose at certain point user will commit his transactions. Some of them will fail. The question is how to redirect the user back to the last page before he committed?

1) Client side caching/refreshing such as JavaScript history object is not allowed. Checking of double submssions would fail you.
2) Technique such as caching the current URL in the HTTPRequest for the "return URL" session before committing a transaction is not going to work either as we simply don't know which one is the "current URL" as a page is an aggregated results of multiple jsps and tiles.

Would be very much appreciate if hints can be provided.

Thanks in advance,
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
Since you are using Struts, if your action defines a forward for where to go on an error, and your action handler did the work to call the database, then the handler would know when the transaction has failed.
Then inside the handler, for the block of code where the failure is acknowledged, have the action forward to the error page, which could be the same page, or the page you used to be.
You could also set some ActionErrors to be displayed

Below is an example of what I mean.
Consider you have a 2 page form, where the flow goes from (home) -> page1 -> page2. and the transaction is committed on page 2.

the struts-config.xml may look like:

<action path="page1" type="edit.Pagae1" name="page1Form" input="tiles.editForm1" scope="session">
<forward name="home" path="tiles.editForm1" />
<forward name="invalid" path="tiles.editForm1" />
<forward name="cancel" path="" />
<forward name="next" path="" />

<action path="page2" type="edit.Page2" name="page2Form" input="tiles.editForm2" scope="session">
<forward name="home" path="tiles.editForm2" />
<forward name="invalid" path="tiles.editForm2" />
<forward name="cancel" path="" />
<forward name="back" path=""/>
<forward name="error" path=""/>
<forward name="finish" path="" />

I guess the forward for "error" in page2 could go to tiles.editForm1 instead of invoking the struts form / handler for again, or could go to some other tiles based error page.

Then, in the execute() within the handler for Page2:

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

try {
// do save of things here, that may fail.
catch (SomeFailureException ex) {
// it failed, we want to go back to the first screen (previous)
// roll back here..
// redirect to the forward we call "error" in page2 action.
return mapping.findForward(error).

// things worked ok,

return mapping.findForward(finish);

Error: Keyboard not attached. Press F1 to continue.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Since you're writing this with Struts, your best answers will probably come from fellow Struts users.

Moving to the Struts forum.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: How to return to the last page in failure
It's not a secret anymore!