This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes retaining checkbox value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "retaining checkbox value" Watch "retaining checkbox value" New topic
Author

retaining checkbox value

Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49
Hello,

How can I retain the checkbox value on a particular screen.

After I navigate back to the screen that contains the checkbox, I want the screen to display the same value of the checkbox as before.

Any help in this regard will be highly appreciated.

Thanks,
Kunal
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you have an ActionForm associated with the form's Action, and if you use either the Struts <html:checkbox> or <html:multibox> tags, Struts will do this for you automatically. It will cause the box to be either checked or unchecked based on the value of the ActionForm property you specify.

If the above is true for your form and it's still not working, post your code (relevant portions of the JSP, ActionForm, Action, struts-config.xml file, etc.) and we'll help you debug it.


Merrill
Consultant, Sima Solutions
Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49
Thanks for your response Merrill.
Let me verify this and get back to you.

Thanks again!
Kunal
Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49
I am using the html checkbox in the jsp.
The following are the code snippets

JSP:



struts-config:


The physicianForm is specified as follows:



In the reset method of the NewBusinessDynaValidatorForm, I reset the checkbox



Could someone point me in the right direction.

Thanks,
Kunal
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It appears to be the code in your reset method that is the problem. It should set the value of "hasPhysician" to false, not true. The reason for this is a quirky behavior of HTML. When a checkbox is checked, HTML sends a parameter to the server with the appropriate name and value. However, when the box is not checked, HTML does not send anything to the server. It's therefore important that your reset method set the value of the property to the value you want it to have if the box is not checked, which in this case is false. That way, if the box is not checked and nothing is sent to the server, it will still have the correct value. If the box is checked and a value is sent to the server, it will override what is set in the reset() method.

If you want to set an initial value of true when the page is first displayed, do so either with by specifying initial="true" in the <form-property> tag in struts-config.xml, or by setting the property to true in the Action class that first forwards to this JSP.
[ February 13, 2007: Message edited by: Merrill Higginson ]
Kunal Sathe
Ranch Hand

Joined: Apr 08, 2005
Posts: 49
Thanks for your response Merrill!
I was finally able to retain the value of the checkbox.

We are using a datamodel and I used the model to store the value of the checkbox and I retrieved it from the same place.

It's now working like a charm.

Thanks for your advise anyways.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: retaining checkbox value
 
Similar Threads
Retaining checked value in selectBooleanCheckbox on enabling
Problem in request.getParameterValues()
Problem While using check boxes
How to validate a boolean field using struts validation
Valid JSP Parameters