Form data getting reset in Action
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jeff_Snow
Posted On:   Monday, January 14, 2002 10:49 AM

How can you modify data on the form in an Action? I've got two pages that use the same form. The first page contains fields to do a search and the second page allows the user to edit the data retrieved from the database. Both pages map to the same Action class and same form bean. In the Action, depending on what the user has done, I call a form.setMethod("something") to change the field. Upon successful completion, control is forwarded back to the second page (refresh if you will) by return (mapping.findForward("success") However, it's as if the setMethod() is being called again on the form and resetting the value back to what was retrieved the first time or what the user    More>>

How can you modify data on the form in an Action?


I've got two pages that use the same form. The first page contains fields to do a search and the second page allows the user to edit the data retrieved from the database. Both pages map to the same Action class and same form bean. In the Action, depending on what the user has done, I call a form.setMethod("something") to change the field. Upon successful completion, control is forwarded back to the second page (refresh if you will) by

			
return (mapping.findForward("success")


However, it's as if the setMethod() is being called again on the form and resetting the value back to what was retrieved the first time or what the user had changed the value to.


What am I doing wrong?

   <<Less

Re: Form data getting reset in Action

Posted By:   Ted_Husted  
Posted On:   Sunday, January 20, 2002 06:26 AM


When you forward from one Action to another, the event
is treated like a new request, and everything happens
all over again. Including the part where the ActionServlet
autopopulates the form from the HTTP request.



If you reuse ActionForms between actions, you must
be able to switch the form into a "read-only" mode., so
that the properties are not overwritten or reset. This is
just a matter of putting a "setReadOnly()" property
on the form, and then having the other setters check
this before passing a value along to its private variable.



Then, just be sure that your reset method goes through
each property's setter method, rather than accessing the
private variable directly. This way calling reset will not
clear the properties if getReadOnly()==true.



Your actions can then call setReadOnly(true) or (false) as
they pass the ActionForm along. The ActionServlet does
check to see if there is already an ActionForm before
creating another, so with the readonly property, this is
a very good way to get Actions to work together.



In the nightly build, there is also a new technique for calling
one Action class form another, to eliminate the baton passing.
But that would be another question :)



HTH -Ted

About | Sitemap | Contact