Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How to set checkbox to checked by default

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, ppl i want to make a checkbox checked on page load, i want to do this in <html:checkbox /> whereas there is no such reference to setting this value as was in the case of <checkbox checked>.
And if the user unchecks the checkbox it should be uncheked and the value to be saved to DB should be 0.

I have tried the procedure suggested on the link https://coderanch.com/t/316968/EJB-JEE/java/make-checkbox-checked-struts-tag

but i havent been successful

what i did was

public int getPublic () {
publicProfile=1;
return publicProfile;
}

in the form bean.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Nathan meant was that JUST BEFORE you enter the page for the first time, you'll want to call the setter on the field that corresponds to the checkbox so the checkbox will be checked when you first enter the page. Usually this is done from an Action, and often so from a SetupAction which will involve minor Action chaining.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initialize your form bean property with the value to be passed on checking Check Box
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vani D Bandargal:
Initialize your form bean property with the value to be passed on checking Check Box



Good idea, but that won't work completely. Following that method, if a user were to deselect the checkbox, the value in the ActionForm will never reflect it because empty checkboxes don't get submitted as a parameter.
 
Vani Bandargal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Marc for your feedBack. We can always overide the reset method of ActionForm to avoid any problems with the check boxes.

I am using similar kind of technique in my current project.
Let me know if my undestanding is not correct
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with that is the reset method gets called after the form gets created. If reset is "unchecking" the value and you are not "rechecking" the value in a Setup Action, the value would be unchecked when you enter the page.

The reset method generally also has no merit unless you are setting your ActionForms to session scope, as it was created to solve "the checkbox problem" for forms in session scope.

I'm surprised that you say you are using it in your project and that you say it works.

Disclaimer: I have been wrong before.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, sorry to reply to older post, this was a problem for me tongight.

Not sure how applicable it is to other implementations, but here is what I did to get this to work.

I have a property in my form bean (formAction) that gets rendered as a hidden field onto the form; some javascript can set formAction.value and then submit the form, my Action handler knows to look in formAction property to get what it should do.

my form has boolean property tabbedView, that is rendered with a checkbox on the html form, that i wanted to be default checked. (but it was not working for me)



So, the JSP has <html:checkbox property="tabbedView"> for the check box,
and the <html:hidden property="formAction" value=""/>

The formAction is then available on the request parameters if the form has been submitted, and unlike checkboxes, it is always available.

Then the reset() in the ActionForm:



So the reset does not clear default checkbox value before it should, but maintains the current state of the checkbox when the form is submitted. (happiness).

I guess you could use have the if() statement in the reset() look for some other property is always available when the form is posted, and is unieuq to this particular form.
One thing to note, as was happening to me, if the formAction hidden property is ALSO available on the previous screen that forwarded to this screen with the default true checkbox, there would be a request parameter present for the formAction, and the feature degenerates to as it was before, where default true property always gets unsolved. (be set in advance, and the reset() would clear it).
In this case, you can either use different (unique to this form) name to inspect for, or use redirects between your forms (if you can use a redirect instead of a forward).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic