You can create two hidden fields for each of the checkboxes. For the onSelect() event of the checkbox, set the value for the corresponding hidden field as "Y". By the way, the default value of the hidden fields is "" so that, when the checkbox is not selected, you still get "" rather than nothing. The names of the hidden fields will be the one, given in the form bean.

You can also code <html:checkbox property="myProperty" value="Y" /> and then in the form's reset() method, code setMyProperty(""). This will have the same effect of putting "Y" in the field if it is checked, and "" in the field if it is unchecked.

What's the use of passing ActionMapping mapping, HttpServletRequest request? can i use a method without it?

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

posted Mar 10, 2005 15:46:00

0

Liz,

The reset() method is defined in the superclass (ActionForm), and is called automatically by the Struts ActionServlet when the form is submitted. What we are doing here is overriding the method, so the signature must match that of the method in the superclass, even if we don't happen to need the parameters.

Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112

posted Mar 10, 2005 16:19:00

0

what may be other reasons if my reset is not working as it should be?

I have done according to what you have said.

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

posted Mar 10, 2005 16:26:00

0

Liz

I'm afraid I don't understand your question. If as you state, your action is getting the correct values, then your reset() method is working properly. You do realize that you don't have to call the reset method, right? It's called for you by Struts.

Please explain in greater detail what is going wrong, and I'll help if I can.

I can see the flow comes in reset method.since it prints the statements. I am checking the values what I am getting by myForm.getImportedValue() in Action class.

1st time when user comes on the form, the checkbox is unchecked. After submit of this form if user comes back to the form, he still can see the checkmark. The problem is that the checkbox in the jsp is not setting to "unchecked" as 1st time (when I go to the form page again after my execute.)

If my user comes back to myForm I should show him that this checkbox is not checked as 1st time.

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

posted Mar 10, 2005 17:23:00

0

Liz,

One of the main advantages of using Struts <html:xxx> tags is that they are controlled by the property in the form bean. You are essentially linking the form element to the data in the ActionForm. In your case, the property importedCheck controls the checkbox. If the property contains "Y" when the form is generated, the checkbox will be checked. If it contains anything else, it will be unchecked.

Therefore, if you want the checkbox to be unchecked, you must set the property in your Action class.

Somewhere in the Action class that forwards to this JSP, you should put the statment:

myForm.setImportedCheck("");

Then, when the JSP is displayed, the checkbox will be unchecked. Likewise, if you wanted the box to be checked, you would code the statement:

That reset needs to be implemented only if ur form bean is in session scope

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

posted Mar 11, 2005 09:54:00

0

Perhaps a word about the reset() method is in order here. In the struts architecture, here is the sequence of events when a user fills out a form and presses the submit button:

The html request is submitted to the server

The Struts ActionServlet is called

ActionServlet either instantiates a new ActionForm or retrieves it from the session scope, depending on the scope you defined.

ActionServlet invokes the reset() method on the ActionForm

ActionServlet populates the properties of ActionForm based on the parameters passed in the request object.

ActionServlet calls validate()

ActionServlet determines which Action class to call based on the information in struts-config.xml, instantiates the class, and calls it's execute() method

The Execute method does it's work and returns an ActionForward object to ActionServlet, which then forwards to the appropriate JSP.

The need for the reset() method exists because html check boxes and radio buttons don't return a value if unchecked. That means that if the property in your ActionForm is "Y", and you uncheck the box, there is no parameter passed in the request to cause another value other than "Y" to be placed in the property, so it remains unchanged. This is not what you want.

To solve this problem, the reset() method allows youto place the value you want in the property if the check box is unchecked. From the sequence of events above, you can see that this will cause the value to be set before the properties are populated. Therefore, if parameter value for this property is not sent, this property will remain in it's unchecked state. If the parameter is sent due to the check box being checked, the property will have it's checked value.

If the ActionForm is in request scope, the ActionForm will be instantiated by ActionServlet each time a request is submitted. However, if the check box is unchecked, the setter for the property will not get called, and the property will be null. Since the desired state for unchecked is "", you still need the reset() method even in request scope.