This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Struts and the fly likes calling reset on the form bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "calling reset on the form bean" Watch "calling reset on the form bean" New topic
Author

calling reset on the form bean

Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hello
What is the syntax to call the reset method on my formBean from the Struts <html:reset>? It seems oddly Escher-esque. How do I specify it so Struts will reflect and get the name of that formBean (possibly one of many on the document) and call it? Is it something that I have to do manually with an onClick event and a custom javascript function that somehow calls the formBean? tia
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The <html:reset> tag does not call the reset method on the ActionForm. The two have nothing to do with each other. The <html:reset> tag produces an HTML <input type="reset"> tag, which renders an reset button. A reset button causes any input fields to revert to their original values.

The reset method on an ActionForm is called automatically by Struts after the ActionForm is instantiated, but before it calls the setters.


Merrill
Consultant, Sima Solutions
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hello Merrill
Yes, I know that and I have changed the scope on my action to be request so it resets and that works. However I want the reset button to work as implied; when I click on it nothing happens. How do I code the syntax, I assume in the onClick, to call the reset on the formBean whenever it is clicked?
Originally posted by Merrill Higginson:
The <html:reset> tag does not call the reset method on the ActionForm. The two have nothing to do with each other. The <html:reset> tag produces an HTML <input type="reset"> tag, which renders an reset button. A reset button causes any input fields to revert to their original values.

The reset method on an ActionForm is called automatically by Struts after the ActionForm is instantiated, but before it calls the setters.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
"Yes, I know that..."

Do you? The reset button is 100% client side. It does not submit your form, interact with any of your actions, or call methods on your "formBean". All it does is reset all the form fields on your page to what they were when the page was first displayed. Note that this does not mean that it will clear all the fields or set them back to some set of predefined default values. In my opinion the reset button is just about the most useless thing in the html specification and I don't see any reason to use it.

- Brent
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hi
Well I guess not. By scope-ing the Action to request, it clears form between submissions. However I need to have the Reset button work. Does that mean I have to fully write a javascript funtion client site and attach it to the html:reset tag? Is there nothing more automated I can use? Cant I use a Validator and have it work to reset the Reset button onClick.
Originally posted by Brent Sterling:
"Yes, I know that..."

Do you? The reset button is 100% client side. It does not submit your form, interact with any of your actions, or call methods on your "formBean". All it does is reset all the form fields on your page to what they were when the page was first displayed. Note that this does not mean that it will clear all the fields or set them back to some set of predefined default values. In my opinion the reset button is just about the most useless thing in the html specification and I don't see any reason to use it.

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Chris Pat:
However I need to have the Reset button work.

The HTML reset button does "work" according to it's limited specified functionality. The trouble is that your definition of "works" seems to be different than that. If you write your own JavaScript code, you are completely in control of what "works" means or doesn't mean, so that may be your best option.

Rather than using <html:reset>, I would use <html:button>. Since you apparently have no use for the original functionality of the reset button, there's no point in using it. The button tag was specifically designed for use with JavaScript. It only does whatever you cause the onclick event to perform.

Sometimes I put a button on a form with the label "reset" when the function I want this button to perform is to reset the values of all input fields to the value they currently have in the database. For this function, the tag I use is <html:cancel>.

This produces a submit button that has some special meaning for Struts. First of all, it tells Struts not to run validation. That is, it doesn't call validate on your ActionForm, nor does it perform framework validations. Secondly, it sends a flag to the Action class. You can call the Action class method isCancelled to determine whether the cancel button was pressed, or whether a regular submit button was pressed. I then put logic in my Action class such that if isCancelled is true, I retrieve the values from the database and forward to the calling JSP.

If this is the type of thing you're looking to do, great. If not, it sounds like an <html:button> with an onclick event calling your own JavaScript function is your best bet.

One other note: If you're using Struts 1.2.9 or above, you must specify cancellable=true when you define the action path in struts-config.xml in order to use the cancel button.
 
 
subject: calling reset on the form bean