File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Uncehecking a checked property Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Uncehecking a checked property" Watch "Uncehecking a checked property" New topic
Author

Uncehecking a checked property

Ergin Er
Ranch Hand

Joined: Sep 06, 2005
Posts: 60
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?
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

It's funny that you asked that, Ergin, because I just answered the problem over here.

Hope my comments there help.


A good workman is known by his tools.
Ergin Er
Ranch Hand

Joined: Sep 06, 2005
Posts: 60
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
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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

Joined: Sep 06, 2005
Posts: 60
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
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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

Joined: Sep 06, 2005
Posts: 60
I was referring to the init method in the Action
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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

Joined: Sep 06, 2005
Posts: 60
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
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uncehecking a checked property