• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Uncehecking a checked property

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

I think this is one problem a lot of people may have encountered, but I couldn't find a stisfying answer.

A user is filling in a registration form that consists out of 3 jsp pages. All those pages are linked to 1 form and one action (dispatchaction). At the first page of the form there are a couple of checkboxes that are checked by default (is a requirement). That happens by setting the values in the form when the properties are declared (I can be boolean or String).
The user may uncheck them if he/she wants to. Of course here lies the problem. When a checkbox is unchecked, it will not be passed to the form as such. So I will allways get the checked values back.
How can I solve this problem accordingly?
 
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
It's funny that you asked that, Ergin, because I just answered the problem over here.

Hope my comments there help.
 
Ergin Er
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

If what is the difference if I set the checkbox values in the Action in stead of in the Form?
If uncheck the checkbox, the value still won't change when I go from first page to the second (I did the check in the action method that leads to second page, and no changed value when I uncheck the boxes).

Here is more detail on what I did in the action:


[ October 26, 2005: Message edited by: Ergin Er ]
[ October 26, 2005: Message edited by: Ergin Er ]
 
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
It looks like you are probably having your ActionForms in session scope (request scope is more commonly accepted). When you are using session scope for your forms you should implement ActionForm's reset method to reset the checkbox value to being unchecked.

Sorry I did not mention this earlier. I didn't want to confuse the people who define ActionForms as request scope (as defined via ActionMapping in struts-config).
 
Ergin Er
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I implemeted the reset method in the form


and I added it tot the constructor of the form. On the init method it first gets reset than populated.
After unchecking 1 checkbox and doing a submit to the next page, the reset method doesn't get called. The form needs to be reset before the data is read from the JSP, but how?
 
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 Ergin Er:

and I added it tot the constructor of the form. On the init method it first gets reset than populated.
After unchecking 1 checkbox and doing a submit to the next page, the reset method doesn't get called. The form needs to be reset before the data is read from the JSP, but how?



You added what to the constructor? The constructor shouldn't need a thing.
What init method? There are no init methods in Struts.

Anyway, here's why it's not working for you...
The reset method is not getting called because its signature should include:
ActionMapping mapping, HttpServletRequest request
 
Ergin Er
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was referring to the init method in the Action
 
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 Ergin Er:
I was referring to the init method in the Action



Gotcha. I forgot you are using DispatchActions.
FYI - you will never have to call the reset method yourself. Struts will do it automatically.

Seems to me you should have everything ironed out by now. Let us know how it all works out.
 
Ergin Er
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc I finally made it work.
As I mentioned, the form was spread over 3 pages (meaning also 3 actionmappings). Since the form would automatically reset the values when I enter each page (And I only needed to reset the values in before entering second page) I added an extra check that will help me reset the values once (I called it String resetOnce = "" That way I can conroll what gets reset and what doesn't.
I still think the Struts development team should add a checked property in the struts html:checkbox tag to make life a little easier.
Thanks dude.
 
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
Glad it works.

html:checkbox actually does have what you're talking about - the value attribute... BUT you will find that it won't solve you problem unless your page will never have validation. You see, when you return to the page after failed validation, the checkbox would automatically check itself the second time, possibly overriding the user's unchecking.

Another solution you could have done (instead of checkonce, which is a decent solution but MIGHT give problems if the first page ever fails validation and returns to the first page a second time) is to place the checkbox's value as a hidden field on your other two pages.
 
reply
    Bookmark Topic Watch Topic
  • New Topic