• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to return to the last page in failure  RSS feed

jon lai
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="home.do" />
<forward name="next" path="page2.do" />

<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="home.do" />
<forward name="back" path="page1.do"/>
<forward name="error" path="page1.do"/>
<forward name="finish" path="home.do" />

I guess the forward for "error" in page2 could go to tiles.editForm1 instead of invoking the struts form / handler for page1.do 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);
Ben Souther
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're writing this with Struts, your best answers will probably come from fellow Struts users.

Moving to the Struts forum.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!