You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pasi Salminen <pa...@profithome.com> on 2000/07/14 11:29:42 UTC

Problems with action forms and checkboxes

Extract from Struts user's guide:

You should note that a "form", in the sense discussed here, does not
necessarily correspond to a single JSP page in the user interface. It is
common in many applications to have a "form" (from the user's
perspective) that extends over multiple pages. Think, for example, of
the wizard style user interface that is commonly used when installing
new applications. Struts encourages you to define a single ActionForm
bean that contains properties for all of the fields, no matter which
page the field is actually displayed on. Likewise, the various pages of
the same form should all be submitted to the same Action Class. If you
follow these suggestions, the page designers can rearrange the fields
among the various pages, with no changes required to the processing
logic in most cases.

Problem:

As said in the manual, struts encourages me to use the same action form
over several pages. Let's assume I have a form with two checkboxes (say
cba and cbb). When entering the page the first time, I set both
checkboxes and submit. Struts then sets both variables, cba and cbb, in
my bean to true. Later the user goes back and deselects the checkbox cba
and submits. However, since only checked values are sent to the server,
struts cannot set the variable in my bean to false (which of course
should happen). What to do in this situation? First I thought I could
subclass the ActionForm (and ActionServlet) and add a method,
prePopulation, to it which could be used to set all checkbox variables
to false. These values would then be overridden by BeanUtils.populate
when the request parameters are populated to the bean. However, this
doesn't work if I have many HTML forms with checkboxes mapped into same
action form, since I have unset all the checkbox (or boolean) variables.
I must know the request and know all the properties which are associated
to it. For me it looks like the user's guide should not encourage the
use of single form over several pages, since it has this kind of
problems. Or am I wrong? Any recommendations?

Greetings,

Paci


Re: Problems with action forms and checkboxes

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
See below.

Pasi Salminen wrote:

> Extract from Struts user's guide:
>
> You should note that a "form", in the sense discussed here, does not
> necessarily correspond to a single JSP page in the user interface. It is
> common in many applications to have a "form" (from the user's
> perspective) that extends over multiple pages. Think, for example, of
> the wizard style user interface that is commonly used when installing
> new applications. Struts encourages you to define a single ActionForm
> bean that contains properties for all of the fields, no matter which
> page the field is actually displayed on. Likewise, the various pages of
> the same form should all be submitted to the same Action Class. If you
> follow these suggestions, the page designers can rearrange the fields
> among the various pages, with no changes required to the processing
> logic in most cases.
>
> Problem:
>
> As said in the manual, struts encourages me to use the same action form
> over several pages. Let's assume I have a form with two checkboxes (say
> cba and cbb). When entering the page the first time, I set both
> checkboxes and submit. Struts then sets both variables, cba and cbb, in
> my bean to true. Later the user goes back and deselects the checkbox cba
> and submits. However, since only checked values are sent to the server,
> struts cannot set the variable in my bean to false (which of course
> should happen). What to do in this situation? First I thought I could
> subclass the ActionForm (and ActionServlet) and add a method,
> prePopulation, to it which could be used to set all checkbox variables
> to false. These values would then be overridden by BeanUtils.populate
> when the request parameters are populated to the bean. However, this
> doesn't work if I have many HTML forms with checkboxes mapped into same
> action form, since I have unset all the checkbox (or boolean) variables.
> I must know the request and know all the properties which are associated
> to it. For me it looks like the user's guide should not encourage the
> use of single form over several pages, since it has this kind of
> problems. Or am I wrong? Any recommendations?
>

There is a definite problem with checkboxes, which has to do with the
fact
that HTML includes the parameter if the checkbox is checked, and does
not
include it at all if it's unchecked.  This occurs even when your form is
a
single page and you go back to redisplay it.

Various solutions have been proposed -- I haven't had a chance to
integrate
a fix for it yet.

>
> Greetings,
>
> Paci

Craig