Two Laptop Bag*
The moose likes Struts and the fly likes Struts and URLs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts and URLs" Watch "Struts and URLs" New topic
Author

Struts and URLs

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Hi all,

I have a jsp page that allows a user to enter their details for creating an account. When the user hits submit I call createNewMember.do - this action class inserts the details of the user into the database. When it has finished it returns a findForward value of "success". Struts config maps this to the userHome.do action class. This page will retrieve user data and send beans to to displayUserHome.jsp. (as it is a new user it will be mostly empty for now).

The problem I am having is that when the displayUserHome.jsp shows, the url that displays is createNewMember.do and not userHome.do - I believe this is the case because I have chained the two action files in struts-config.xml. This means that if the user refreshes the screen the logic to create a new user is executed again!

Is there an attribute that I can use to prevent this in the struts-config.xml file.

Thank you for your help.
david
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
If your concern is with "duplicate submission of a form" (user pressing refresh) then struts does provide something called "Tokens" which you might be interested in using.

Unfortunately, I have never used it, but I remember seeing a post about it in this forum. Try searching for it using "Token". Hopefully someone more knowledgeable will come along and help you.

Sheldon Fernandes
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by david allen:
The problem I am having is that when the displayUserHome.jsp shows, the url that displays is createNewMember.do and not userHome.do - I believe this is the case because I have chained the two action files in struts-config.xml. This means that if the user refreshes the screen the logic to create a new user is executed again!
Try setting the attribute redirect="true" for the <forward/> that takes the user from createNewMember.do to userHome.do. That'll effectively do a HTTP 302 redirect instead of a server-side forward and the browser's location bar should be showing "userHome.do".


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Hi

There are two ways as the above two mentioned ...............in the second scenario where if u make redirect=true ur request attributes wont be available to the next jsp/action as it makes a roung trip to the client and struts provide tokens which store a variable in session and this eliminates the duplicate submission of the form

so depending on the data u can decide up on which to use..i feel redirect is better if u dont have much data to store in request as this will also avoid resubmission if the user clicks back button also
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts and URLs