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" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> 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
> >
>
>