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 How to save state of a form. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to save state of a form." Watch "How to save state of a form." New topic
Author

How to save state of a form.

Kim Chew
Greenhorn

Joined: Nov 01, 2005
Posts: 2
Hello folks,

I have a scenario like this,
Page A : Some text boxes and check boxes.
Link to Page B.
A "Save" and a "Cancel" button.

Page B : Some text boxes and check boxes.
A "Save" and a "Cancel" button.

My question is like this, if the user typed in something in the text boxes
on Page A, then clicked the link to Page B. In Page B, the user clicked
the "Cancel" button which bring him back to Page A. How do I preserve the
changes the user made in Page A (Without using the "back" button,of course)?

Thanks in advance!

Kim
SuprajaY Yasoda
Greenhorn

Joined: Oct 17, 2005
Posts: 17
It will be better to insert the values into the database once Save button is pressed. On clicking of Cancel button user will see the data fetched from database
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The best way I know to do this would be to change the link so that it calls a javascript function using the onclick event, or changing the link to "javascript:myfunction()". In that function put code that changes the action of the form and submits it. A link does not preserve the state of the form before loading another page, while a form submission does.

So, you might create a new action mapping that simply forwards to page B. Then, in your javascript function, change the form's action property to the new action mapping and submit the form. Struts will then populate your form bean with whatever the user entered before clicking the link. If the user goes back to Page A, the fields will be populated.


Merrill
Consultant, Sima Solutions
Deepak Tewari
Greenhorn

Joined: Aug 04, 2005
Posts: 10
I too agree with Merrill Higginson the thing u can do is on the click of the button call a java script that submits the page and in the action class save the data that is the form obtained, in the session if u dont want to save the data parmenantly ie (submit of the page) other wise submit the page hence since the form is saved in the session u can populate the page again on coming back to the page again.


Deepak Tewari
Kim Chew
Greenhorn

Joined: Nov 01, 2005
Posts: 2
Merrill, thank you very much for your response.
I am not very well versed in Javascript, so while I understand the
mecahnism but I don't know how to "change the form's action property to the
new action mapping and submit the form." in the java script function.

So please allow me shamelessly to ask for a example snippet.

Many thanks.

Kim
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's a sample page that shows how to do it.
 
GeeCON Prague 2014
 
subject: How to save state of a form.