• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Set default checkbox value

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to set a default checked value for a checkbox. Here is my code in jsp:

However, when the page is loaded, the checkbox is NOT checked at all.

Can anybody help me to solve it?

Thanks in advance.

X. Li
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked the Struts documentation and found the following information on the value attribute for <html:checkbox>:

"The value to be transmitted if this checkbox is checked when the form is submitted. If not specified, the value 'on' will be returned."

What this means is that by writing a value attribute you are only specifying the value to be returned when the form is submitted, not the value when it is displayed.

A struts checkbox will be checked or unchecked based on the value of the property when the page is built. This means that you have to set the value in the Form bean before displaying the page.

If you want to set the value when the page is displayed, do so in the Action class that is called before this form is displayed with a statement such as

myForm.setMarried(true);

This assumes that the married attribute is a boolean.
 
X. Li
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Merrill. Your suggestion is great: it worked.

X. Li
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One question then remains however:

After successfully setting the checkbox to true by default, how do you handle the case of the checkbox being deselected when the user submits the form? Since HTML forms submit *no value* for an unselected checkbox, there is no "false" submitted to update the form; and the corresponding checkbox attribute remains true, even if the checkbox was deselected and *should* now be set to false.

Am I missing something here? Is there some workaround for this? I am aware of the "reset" method, but does setting the value to "false" there help in this case?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hal,

Your question is a good one. Checkboxes and radio buttons are tricky in that they don't return a value if unchecked. This fact is the main reason the reset() method was included as a part of the action form. In fact, resetting the value to false in the reset() method is the workaround for this. In the case at hand, putting the statement "setMarried(false);" in the reset() method for the form will ensure that the value is false if unchecked, and true if checked. If you have a group of checkboxes or radio buttons that return an array of values, the thing to do is set the String array to an empty array as in "setMaritalStatus(new String[0]);"

Merrill
[ March 04, 2005: Message edited by: Merrill Higginson ]
 
Hal Taylor
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,

Thanks for the feedback. I understand how to use reset() to set checkbox values to false unless checked on the form. However, the situation gets tricky if the checkbox should *default* to try (pre-checked on page load).

The only solution I found was:

- Use the reset() method to set the checkbox to "true".
- In the jsp, include a hidden field with the same name as the checkbox property, and the value "false".

In this case, the checkbox will load as checked. The server will read the *first* value it finds for the property, and ignore subsequent values with t he same property name. So, if the checbox is still checked on submit, the "true" value will be read, and the following "false" from the hidden field will be ignored. If the checkbox is unchecked on submit, the hidden field with value "false" will be used to set the property.

This works in my environment. However, I'm not sure what specs say about behavior in the case of multiple values submitted for the same field, and this *may* vary by environment/container. So I'm not thrilled with this solution, either, but I can't find anything else that works better...
 
Hey! You're stepping on my hand! Help me tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic