You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Thompson <mr...@instanton.com> on 2001/06/16 00:16:43 UTC

Can someone clarify the following:

WARNING: In order to correctly recognize unchecked checkboxes, the ActionForm bean associated with this form must include a statement setting the corresponding boolean property to false in the reset() method.

Does this mean that if I use check boxes I have to stash the current state of my properties somewhere.  How do I know which property has been "unchecked"?


    --m



Michael R. Thompson
http://www.instanton.com
512.439.3815

Re:

Posted by Martin Cooper <ma...@tumbleweed.com>.
If, by defaulting to true, you mean setting it to true when the page is
displayed, yes, you can do that. In your Action, set the property in your
form bean to true, and the checkbox will be displayed checked.

When the form is submitted, Struts will call your form bean's reset()
method, where you should set the property to false. If the checkbox was
checked when the form was submitted, there will be a query parameter
corresponding to it, and Struts will set the form bean property to true. If
the checkbox was not checked, there will be no corresponding query
parameter, so Struts will not set the property at all. The property will
then be false - as set by your reset() method - which is correct.

Hope this helps.

--
Martin Cooper


----- Original Message -----
From: "Rod Schmidt" <ro...@qwest.net>
To: <st...@jakarta.apache.org>
Sent: Saturday, June 16, 2001 10:55 PM
Subject: Re: <html:checkbox>


> Does this mean you can't default a checkbox to true? If you can, how do
you
> do it?
>
> Thanks,
> Rod Schmidt
>
> ----- Original Message -----
> From: "Ted Husted" <hu...@apache.org>
> To: <st...@jakarta.apache.org>
> Sent: Friday, June 15, 2001 5:45 PM
> Subject: Re: <html:checkbox>
>
>
> > They are actually "cached" in the request. It's just that with boolean
> > checkboxes, you should reset them them to false first, or there are side
> > effects. You really, really don't have to do anything else.
> >
> > It's important to remember that there is not a direct connection between
> > the HTML form and Struts. Everything has to go through HTTP. When the
> > form is submitted, all the properties are sent as parameters to the
> > request by HTTP. Struts then "catches" them and puts them back into the
> > ActionForm.
> >
> > What can happen with checkboxes is that if they get unchecked, the
> > browser won't send them back, and so Struts has no way to set them true
> > or false. By setting them to false when the form is initialized and to
> > false again when the form is reset, you get consistent results.
> > Otherwise, if they unchecked the box, and the browser didn't send it
> > back, the setting on your box may be indeterminate.
> >
> > Mike Thompson wrote:
> > >
> > > So this means that I have to cache the original state of all my
checkbox
> > > referenced vars so I can set them back in the reset method? :(
> > >     --m
> >
>



Re:

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sat, 16 Jun 2001, Rod Schmidt wrote:

> Does this mean you can't default a checkbox to true? If you can, how do you
> do it?
> 

When you create the ActionForm bean instance, set the corresponding
property to true.  You can do this in a couple of ways:

* Initialize the instance variable to true inside the bean as you
  declare it.  That way, this checkbox will *always* be initialized
  to checked.

* Create the form bean yourself in an action (instead of letting Struts
  do it automatically), and call the setFoo(true) method on it.

The Struts example application uses the latter technique, for example, on
the SubscriptionForm bean.  In the Edit Subscription action, the form bean
is created and the state of the autoConnect property is copied from the
underlying database bean.

Note that, no matter *what* you initialize it to, you must always call
setFoo(false) in the reset() method.

> Thanks,
> Rod Schmidt
> 

Craig


> ----- Original Message -----
> From: "Ted Husted" <hu...@apache.org>
> To: <st...@jakarta.apache.org>
> Sent: Friday, June 15, 2001 5:45 PM
> Subject: Re: <html:checkbox>
> 
> 
> > They are actually "cached" in the request. It's just that with boolean
> > checkboxes, you should reset them them to false first, or there are side
> > effects. You really, really don't have to do anything else.
> >
> > It's important to remember that there is not a direct connection between
> > the HTML form and Struts. Everything has to go through HTTP. When the
> > form is submitted, all the properties are sent as parameters to the
> > request by HTTP. Struts then "catches" them and puts them back into the
> > ActionForm.
> >
> > What can happen with checkboxes is that if they get unchecked, the
> > browser won't send them back, and so Struts has no way to set them true
> > or false. By setting them to false when the form is initialized and to
> > false again when the form is reset, you get consistent results.
> > Otherwise, if they unchecked the box, and the browser didn't send it
> > back, the setting on your box may be indeterminate.
> >
> > Mike Thompson wrote:
> > >
> > > So this means that I have to cache the original state of all my checkbox
> > > referenced vars so I can set them back in the reset method? :(
> > >     --m
> >
> 
> 


Re:

Posted by Rod Schmidt <ro...@qwest.net>.
Does this mean you can't default a checkbox to true? If you can, how do you
do it?

Thanks,
Rod Schmidt

----- Original Message -----
From: "Ted Husted" <hu...@apache.org>
To: <st...@jakarta.apache.org>
Sent: Friday, June 15, 2001 5:45 PM
Subject: Re: <html:checkbox>


> They are actually "cached" in the request. It's just that with boolean
> checkboxes, you should reset them them to false first, or there are side
> effects. You really, really don't have to do anything else.
>
> It's important to remember that there is not a direct connection between
> the HTML form and Struts. Everything has to go through HTTP. When the
> form is submitted, all the properties are sent as parameters to the
> request by HTTP. Struts then "catches" them and puts them back into the
> ActionForm.
>
> What can happen with checkboxes is that if they get unchecked, the
> browser won't send them back, and so Struts has no way to set them true
> or false. By setting them to false when the form is initialized and to
> false again when the form is reset, you get consistent results.
> Otherwise, if they unchecked the box, and the browser didn't send it
> back, the setting on your box may be indeterminate.
>
> Mike Thompson wrote:
> >
> > So this means that I have to cache the original state of all my checkbox
> > referenced vars so I can set them back in the reset method? :(
> >     --m
>


Re:

Posted by Ted Husted <hu...@apache.org>.
They are actually "cached" in the request. It's just that with boolean
checkboxes, you should reset them them to false first, or there are side
effects. You really, really don't have to do anything else. 

It's important to remember that there is not a direct connection between
the HTML form and Struts. Everything has to go through HTTP. When the
form is submitted, all the properties are sent as parameters to the
request by HTTP. Struts then "catches" them and puts them back into the
ActionForm. 

What can happen with checkboxes is that if they get unchecked, the
browser won't send them back, and so Struts has no way to set them true
or false. By setting them to false when the form is initialized and to
false again when the form is reset, you get consistent results.
Otherwise, if they unchecked the box, and the browser didn't send it
back, the setting on your box may be indeterminate.

Mike Thompson wrote:
> 
> So this means that I have to cache the original state of all my checkbox
> referenced vars so I can set them back in the reset method? :(
>     --m

Re:

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 15 Jun 2001, Mike Thompson wrote:

> So this means that I have to cache the original state of all my checkbox
> referenced vars so I can set them back in the reset method? :(
>     --m
> 

No.

In the reset() method, you *always* set the property to false.

Craig


Re:

Posted by Mike Thompson <mr...@instanton.com>.
So this means that I have to cache the original state of all my checkbox
referenced vars so I can set them back in the reset method? :(
    --m

----- Original Message -----
From: "Ted Husted" <hu...@apache.org>
To: <st...@jakarta.apache.org>
Sent: Friday, June 15, 2001 5:56 PM
Subject: Re: <html:checkbox>


> The underlying problem is that if a HTML widget has no value, then it
> does not get submitted at all. Which includes an unchecked checkbox.
>
> Struts needs to set the ActionForm properties from the submit, but if
> the checkbox isn't there it can't, and so the checkbox could end up null
> (rather than false).
>
> Struts calls reset before setting the properties from the submit, so by
> setting the checkbox to the default state (be it true or false), it will
> either remain that way or be changed to reflect the user gesture.
>
> Mike Thompson wrote:
> >
> > Still confused.  So in my reset method, I run through all my beans and
if
> > the property is false, I set it false again?  Does not compute?  I guess
I'm
> > not sure how to get the "corresponding" boolean property?
> >
> > What I have is a list of checkboxes that may be initialized to true or
false
> > depending on the beans state.  When a user "unchecks" one of these, how
do I
> > know which property to set to false in the reset method?


Re:

Posted by Ted Husted <hu...@apache.org>.
The underlying problem is that if a HTML widget has no value, then it
does not get submitted at all. Which includes an unchecked checkbox. 

Struts needs to set the ActionForm properties from the submit, but if
the checkbox isn't there it can't, and so the checkbox could end up null
(rather than false). 

Struts calls reset before setting the properties from the submit, so by
setting the checkbox to the default state (be it true or false), it will
either remain that way or be changed to reflect the user gesture.

Mike Thompson wrote:
> 
> Still confused.  So in my reset method, I run through all my beans and if
> the property is false, I set it false again?  Does not compute?  I guess I'm
> not sure how to get the "corresponding" boolean property?
> 
> What I have is a list of checkboxes that may be initialized to true or false
> depending on the beans state.  When a user "unchecks" one of these, how do I
> know which property to set to false in the reset method?

Re:

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 15 Jun 2001, Mike Thompson wrote:

> Still confused.  So in my reset method, I run through all my beans and if
> the property is false, I set it false again?  Does not compute?  I guess I'm
> not sure how to get the "corresponding" boolean property?
> 
> What I have is a list of checkboxes that may be initialized to true or false
> depending on the beans state.  When a user "unchecks" one of these, how do I
> know which property to set to false in the reset method?
>     --m
> 

Sorry about that.  Let me try again.

When you create a form bean (say, in an Action that saves it in request
scope and then forwards to the page for display), you should set the
boolean properties to whatever state you want the checkbox to be
initialized to.

In your reset() method, you should set them all to false.

For example, let's say you have a form page like this:

  <html:form action="/foo">
    ...
    <html:checkbox property="bar"/>
    ...
  </html:form>

Then, in the corresponding form bean, you would have something like this:

  public class FooFormBean extends ActionForm {

    private boolean bar;

    ...

    public boolean getBar() {
      return (this.bar);
    }

    public void setBar(boolean bar) {
      this.bar = bar;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
      bar = false;
    }

  }

The match-up between the field name on the screen and the property name in
the bean is performed by having the same base name
(i.e. property="bar" means you are talking about the property managed by
getBar() and setBar()).

Craig McClanahan


> 
> 
> ----- Original Message -----
> From: "Craig R. McClanahan" <cr...@apache.org>
> To: <st...@jakarta.apache.org>
> Sent: Friday, June 15, 2001 5:35 PM
> Subject: Re: <html:checkbox>
> 
> 
> >
> >
> > On Fri, 15 Jun 2001, Mike Thompson wrote:
> >
> > > Can someone clarify the following:
> > >
> > > WARNING: In order to correctly recognize unchecked checkboxes, the
> > > ActionForm bean associated with this form must include a statement
> > > setting the corresponding boolean property to false in the reset()
> > > method.
> > >
> > > Does this mean that if I use check boxes I have to stash the current
> > > state of my properties somewhere.  How do I know which property has
> > > been "unchecked"?
> > >
> >
> > The assumption is that you are using a boolean property to correspond to
> > the checkbox field.  Due to the wierd way that HTML submits work, you have
> > to take special precautions on *all* your checkbox-based fields.
> >
> > With Struts, all you have to do is set the corresponding boolean property
> > to false in your reset() method.  If the checkbox was actually checked,
> > the automatic property setting that is done by the controller servlet will
> > cause it to be set back to true.  If the checkbox was not checked, the
> > boolean property will remain false.  That way, when you get this property
> > in your action, it will always reflect the state of the checkbox that the
> > user sees.
> >
> > >
> > >     --m
> > >
> > >
> > >
> > > Michael R. Thompson
> > > http://www.instanton.com
> > > 512.439.3815
> > >
> >
> > Craig
> >
> 
> 


Re:

Posted by Mike Thompson <mr...@instanton.com>.
Still confused.  So in my reset method, I run through all my beans and if
the property is false, I set it false again?  Does not compute?  I guess I'm
not sure how to get the "corresponding" boolean property?

What I have is a list of checkboxes that may be initialized to true or false
depending on the beans state.  When a user "unchecks" one of these, how do I
know which property to set to false in the reset method?
    --m



----- Original Message -----
From: "Craig R. McClanahan" <cr...@apache.org>
To: <st...@jakarta.apache.org>
Sent: Friday, June 15, 2001 5:35 PM
Subject: Re: <html:checkbox>


>
>
> On Fri, 15 Jun 2001, Mike Thompson wrote:
>
> > Can someone clarify the following:
> >
> > WARNING: In order to correctly recognize unchecked checkboxes, the
> > ActionForm bean associated with this form must include a statement
> > setting the corresponding boolean property to false in the reset()
> > method.
> >
> > Does this mean that if I use check boxes I have to stash the current
> > state of my properties somewhere.  How do I know which property has
> > been "unchecked"?
> >
>
> The assumption is that you are using a boolean property to correspond to
> the checkbox field.  Due to the wierd way that HTML submits work, you have
> to take special precautions on *all* your checkbox-based fields.
>
> With Struts, all you have to do is set the corresponding boolean property
> to false in your reset() method.  If the checkbox was actually checked,
> the automatic property setting that is done by the controller servlet will
> cause it to be set back to true.  If the checkbox was not checked, the
> boolean property will remain false.  That way, when you get this property
> in your action, it will always reflect the state of the checkbox that the
> user sees.
>
> >
> >     --m
> >
> >
> >
> > Michael R. Thompson
> > http://www.instanton.com
> > 512.439.3815
> >
>
> Craig
>


Re:

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 15 Jun 2001, Mike Thompson wrote:

> Can someone clarify the following:
> 
> WARNING: In order to correctly recognize unchecked checkboxes, the
> ActionForm bean associated with this form must include a statement
> setting the corresponding boolean property to false in the reset()
> method.
> 
> Does this mean that if I use check boxes I have to stash the current
> state of my properties somewhere.  How do I know which property has
> been "unchecked"?
> 

The assumption is that you are using a boolean property to correspond to
the checkbox field.  Due to the wierd way that HTML submits work, you have
to take special precautions on *all* your checkbox-based fields.

With Struts, all you have to do is set the corresponding boolean property
to false in your reset() method.  If the checkbox was actually checked,
the automatic property setting that is done by the controller servlet will
cause it to be set back to true.  If the checkbox was not checked, the
boolean property will remain false.  That way, when you get this property
in your action, it will always reflect the state of the checkbox that the
user sees.

> 
>     --m
> 
> 
> 
> Michael R. Thompson
> http://www.instanton.com
> 512.439.3815
> 

Craig