You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Anthony N. Frasso" <af...@yahoo.com> on 2006/08/28 23:51:13 UTC

Dynamically Initializing Checkbox Value

Hello all, and thanks for your help in advance.

I have a relatively simple JSP that I'm trying to
present.  I have a bean that is accessible as an
attribute in the request, as well as a form bean for
the form on this page.  As an example, here is one of
the input components for the form:

<html:text property="name" value="${bean.name}" />

This takes the value of the "name" property in the
bean and initializes the text box with this value. 
When the form is submitted, it takes the value from
the text box and places it into the "name" property of
the form bean.

I'd like to do something similar with a checkbox. 
However, I don't see any way to initialize the
checkbox.  The value property works in a different way
than in most of the other form input tags.  How would
I go about accomplishing this?

Thanks again,
Anthony Frasso

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Dynamically Initializing Checkbox Value

Posted by Laurie Harper <la...@holoweb.net>.
Anthony N. Frasso wrote:
>> Do you need one checkbox or a series of checkboxes?
> 
> Just one. :)
> 
>> For one checkbox you write something like this (from
>> the article):
>> <html:checkbox property="married" value="yes"/>
> 
> As I understand it, what this tag does is place the
> value "yes" into the property "married" of the form
> bean associated with this HTML form (actually, "yes"
> and "true" for checkboxes will simply place a "true"
> boolean value in the boolean property "married"). 
> What it does not do, however, is have the form
> initialized to have this checkbox set as true.  What I
> need is something sort of like the following:
> 
> <html:checkbox property="married" value="yes"
> initialValue="${myBean.married}" />

If you have property="married" and value="yes", then the checkbox will 
be selected iff ${myBean.married} has the value "yes". If the property 
doesn't have the value "yes", the checkbox will be cleared. In either 
case, if the checkbox is selected when the form is submitted, the 
myBean.married property will be set to whatever you specifed in the 
'value' attribute.

> Does this clear up what I'm looking for?
> 
> The generated HTML should look like:
> 
> <input type="checkbox" checked="true">
> 
> where the value of the "checked" parameter is the
> evaluation of "${myBean.married}".

Provided your specified 'value' and the actual property value are the 
same, you'll get what you want. Note that there's a special 
consideration for checkboxes, though: if a checkbox is de-selected when 
the form is submitted, *no* URL parameter will be included for it. As a 
result, Struts has no way to automatically clear your form property when 
the checkbox is de-selected.

The solution to this is to have your form's reset() method clear the 
form property to whatever value you choose to represent the de-selected 
state (e.g. "no"). Before you display the page, you should set the 
property to "yes" or "no" as appropriate, to get the checkbox 
appropriately selected on the page. The reset() method takes care of 
making sure the property is 'switched off', and the normal form submit 
processing takes care of making sure it's 'switched on' again if the 
checkbox was selected.

I hope that didn't add more confusion than clarification :-)

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Dynamically Initializing Checkbox Value

Posted by "Anthony N. Frasso" <af...@yahoo.com>.
> Do you need one checkbox or a series of checkboxes?

Just one. :)

> For one checkbox you write something like this (from
> the article):
> <html:checkbox property="married" value="yes"/>

As I understand it, what this tag does is place the
value "yes" into the property "married" of the form
bean associated with this HTML form (actually, "yes"
and "true" for checkboxes will simply place a "true"
boolean value in the boolean property "married"). 
What it does not do, however, is have the form
initialized to have this checkbox set as true.  What I
need is something sort of like the following:

<html:checkbox property="married" value="yes"
initialValue="${myBean.married}" />

Does this clear up what I'm looking for?

The generated HTML should look like:

<input type="checkbox" checked="true">

where the value of the "checked" parameter is the
evaluation of "${myBean.married}".

Thanks again for your help!  I am reviewing the code
samples on the site now.

Regards,
Anthony Frasso

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Dynamically Initializing Checkbox Value

Posted by Michael Jouravlev <jm...@gmail.com>.
Do you need one checkbox or a series of checkboxes?

For one checkbox you write something like this (from the article):
<html:checkbox property="married" value="yes"/>

For a group of checkbox you can create several checkbox HTML elements
with the same name (this is what <html:multibox> does for you).

Looking at the example from the article, "customers" is an ArrayList
of Customer object, they are queued to request object at every HTTP
request, but they can be stored in the session as well and nothing
will change. This is just a list of customers, their properties do not
change. The example uses customer IDs to select/deselect checkboxes
that correspond to customers.

When the following is processed,

  <html:multibox property="control">
    <bean:write name="customer" property="id"/>
  </html:multibox>

the <input type="checkbox" name="control" ...> are generated. They
have "value" attribute provided by <bean:write> tag. How come? Because
in this particular case <bean:write> is the body of <html:multibox>
tag. See description of <html:multibox> tag for explanation:
http://struts.apache.org/1.x/struts-taglib/tlddoc/html/multibox.html .

Then you check some boxes, and browser submits something like this:
http://mysite/myboxes.do?control=001&control=003

Struts initializes multiSelectForm ActionForm, creates "control" array
as its property, and fills its elements with the values above. Next
time the JSP is displayed, these values are matched against IDs of
Customer objects, and appropriate checkboxes are marked as checked.

I agree it is kinda convoluted ;-)

The article has downloadable samples and the source code, study it, it
is quite helpful.

Michael.

On 8/28/06, Anthony N. Frasso <af...@yahoo.com> wrote:
> Thanks, I appreciate the quick response.
> Unfortunately, I'm not sure that this addresses my
> issue, unless I've misunderstood something.  It
> doesn't really mention how to populate a checkbox with
> a value... but it does provide the following:
>
> <logic:iterate id="customer" name="customers">
>   <html:multibox property="control">
>     <bean:write name="customer" property="id"/>
>   </html:multibox>
>   <bean:write name="customer" property="fullName"/>
>   <br>
> </logic:iterate>
>
> This creates a checkbox for each customer in a
> collection of customers.  It supposedly creates the
> following html:
>
> <input type="checkbox" name="control" value="001"
> checked="checked">
> John Doe
> <br>
> <input type="checkbox" name="control" value="002">
> Peter Smith
> <br>
>
> What I don't understand is how it determines that John
> Doe should have his checkbox checked, but not Peter
> Smith.
>
> Do you have any ideas?
>
> Thanks,
> Anthony Frasso
>
> --- Michael Jouravlev <jm...@gmail.com> wrote:
>
> > On 8/28/06, Anthony N. Frasso <af...@yahoo.com>
> > wrote:
> > > Hello all, and thanks for your help in advance.
> > >
> > > I have a relatively simple JSP that I'm trying to
> > > present.  I have a bean that is accessible as an
> > > attribute in the request, as well as a form bean
> > for
> > > the form on this page.  As an example, here is one
> > of
> > > the input components for the form:
> > >
> > > <html:text property="name" value="${bean.name}" />
> > >
> > > This takes the value of the "name" property in the
> > > bean and initializes the text box with this value.
> > > When the form is submitted, it takes the value
> > from
> > > the text box and places it into the "name"
> > property of
> > > the form bean.
> > >
> > > I'd like to do something similar with a checkbox.
> > > However, I don't see any way to initialize the
> > > checkbox.  The value property works in a different
> > way
> > > than in most of the other form input tags.  How
> > would
> > > I go about accomplishing this?
> > >
> > > Thanks again,
> > > Anthony Frasso
> >
> > The first entry that comes up in Google when
> > searching for "struts
> > <html:checkbox>":
> >
> http://javaboutique.internet.com/tutorials/strutsform/
> >
> >
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Dynamically Initializing Checkbox Value

Posted by "Anthony N. Frasso" <af...@yahoo.com>.
Thanks, I appreciate the quick response. 
Unfortunately, I'm not sure that this addresses my
issue, unless I've misunderstood something.  It
doesn't really mention how to populate a checkbox with
a value... but it does provide the following:

<logic:iterate id="customer" name="customers">
  <html:multibox property="control">
    <bean:write name="customer" property="id"/>
  </html:multibox> 
  <bean:write name="customer" property="fullName"/>
  <br>
</logic:iterate>

This creates a checkbox for each customer in a
collection of customers.  It supposedly creates the
following html:

<input type="checkbox" name="control" value="001"
checked="checked"> 
John Doe
<br>
<input type="checkbox" name="control" value="002"> 
Peter Smith
<br> 

What I don't understand is how it determines that John
Doe should have his checkbox checked, but not Peter
Smith.

Do you have any ideas?

Thanks,
Anthony Frasso

--- Michael Jouravlev <jm...@gmail.com> wrote:

> On 8/28/06, Anthony N. Frasso <af...@yahoo.com>
> wrote:
> > Hello all, and thanks for your help in advance.
> >
> > I have a relatively simple JSP that I'm trying to
> > present.  I have a bean that is accessible as an
> > attribute in the request, as well as a form bean
> for
> > the form on this page.  As an example, here is one
> of
> > the input components for the form:
> >
> > <html:text property="name" value="${bean.name}" />
> >
> > This takes the value of the "name" property in the
> > bean and initializes the text box with this value.
> > When the form is submitted, it takes the value
> from
> > the text box and places it into the "name"
> property of
> > the form bean.
> >
> > I'd like to do something similar with a checkbox.
> > However, I don't see any way to initialize the
> > checkbox.  The value property works in a different
> way
> > than in most of the other form input tags.  How
> would
> > I go about accomplishing this?
> >
> > Thanks again,
> > Anthony Frasso
> 
> The first entry that comes up in Google when
> searching for "struts
> <html:checkbox>":
>
http://javaboutique.internet.com/tutorials/strutsform/
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Dynamically Initializing Checkbox Value

Posted by Michael Jouravlev <jm...@gmail.com>.
On 8/28/06, Anthony N. Frasso <af...@yahoo.com> wrote:
> Hello all, and thanks for your help in advance.
>
> I have a relatively simple JSP that I'm trying to
> present.  I have a bean that is accessible as an
> attribute in the request, as well as a form bean for
> the form on this page.  As an example, here is one of
> the input components for the form:
>
> <html:text property="name" value="${bean.name}" />
>
> This takes the value of the "name" property in the
> bean and initializes the text box with this value.
> When the form is submitted, it takes the value from
> the text box and places it into the "name" property of
> the form bean.
>
> I'd like to do something similar with a checkbox.
> However, I don't see any way to initialize the
> checkbox.  The value property works in a different way
> than in most of the other form input tags.  How would
> I go about accomplishing this?
>
> Thanks again,
> Anthony Frasso

The first entry that comes up in Google when searching for "struts
<html:checkbox>":
http://javaboutique.internet.com/tutorials/strutsform/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org