struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From don clasen <dcla...@yahoo.com>
Subject Re: Checkbox
Date Wed, 14 Jun 2000 11:56:38 GMT
See comments below.

--- Ralph Schaer <schaer@ess.ch> wrote:
> Nightly Build 2000/06/13
>
> >The initial thinking was that a <struts:checkbox>
> would represent the value
> of a
> >single boolean property in your bean.
>
> OK. Now I understand. But there are still a few
> problems.
>
>
> example 1
> ---------
>
> JSP:  <struts:checkbox
> name="administrator">administrator</struts:checkbox>
>
> Source: TestForm implements ActionForm
>
>   private boolean admin = false;
>
>   public boolean getAdministrator() {
>     return admin;
>   }
>
>   public void setAdministrator(boolean admin) {
>     this.admin = admin;
>   }
>
> The user select the checkbox and press the submit
> button
> The browser sends the following request to the
> server:
>
http://localhost:8080/test.do?submit=OK&administrator=on
>
> The admin property in TestForm is still false
> because "on" is not mapped to
> true
>
> This is easy to fix. After line 125 in
> BeanUtils.java add the follwing code:
>      else if (value.equalsIgnoreCase("on"))
>         return (new Boolean(true));
>
>
> With this fix example 1 is working
>
>
> example 2
> ---------
>
> JSP: same as example 1
> Source: private boolean admin = true;
>
> The checkbox is now selected. The user DESELECT the
> checkbox and press the
> submit button
> The browser sends the follwing request to the
> server:
> http://localhost:8080/test.do?submit=OK
>
> The admin property is still true because there is no
> "administrator" in the
> query string.
> Thats very annoying that the browser doesn't send
> something like
> "administrator=off"
>
> I have no idea how to handle this case.
>
>



I don't understand why we don't just map any non-null
value to true and any null value to false. The browser
includes the checkbox in the request parameters only
if it is checked (thus request.getParameter() will not
be null) and does not include it if the checkbox is
checked (and request.getParamter() will return null).

With a checkbox, the actual value is irrelevant in the
incoming request, it's simply either included (on (not
null) ==> true) or not included (off (null) ==>
false). If you're depending on the actal value, you
should be using a radio button control.


>
> example 3a
> ----------
>
> more than one checkbox.
> with existing tag
>
> JSP: <struts:checkbox
> name="blue">blue</struts:checkbox>
>      <struts:checkbox
> name="red">red</struts:checkbox>
>      <struts:checkbox
> name="green">green</struts:checkbox>
>      <struts:checkbox
> name="yellow">yellow</struts:checkbox>
>      <struts:checkbox
> name="black">black</struts:checkbox>
>
> Source:
>   private boolean blue = false;
>
>   public boolean getBlue() {
>     return blue;
>   }
>
>   public void setBlue(boolean blue) {
>     this.admin = blue;
>   }
>
>   //Same for red, green, yellow, black
>
> Disadvantag: Every checkbox needs a property and a
> get/set method. Adding a
> new checkbox to the jsp page
>              needs a code change in the Form
>


This example screams out "Use a radio button named
color, not a checkbox".



>
> example 3b
> ----------
>
> My suggestion: mapping checkbox to String[]
>
> JSP: <struts:checkbox
> name="colour">blue</struts:checkbox>
>      <struts:checkbox
> name="colour">red</struts:checkbox>
>      <struts:checkbox
> name="colour">green</struts:checkbox>
>      <struts:checkbox
> name="colour">yellow</struts:checkbox>
>      <struts:checkbox
> name="colour">black</struts:checkbox>
>
> Source:
>   private String[] colour = null;
>
>   public String[] getColour() {
>     return colour;
>   }
>
>   public void setColour(String[] colour) {
>     this.colour = colour;
>   }
>
> This is the way JSP maps checkboxes to java with the
> setProperty tag (<jsp:setProperty name="test"
> property="*"/>)
>
>
> !!Also this approach has the problem mentioned in
> example 2!!!
>
>


See my comment on the last example. If a page
developer uses checkboxes as in this example instead
of radios, I'm not sure you'd even get consistent
results across browsers. I know IE doesn't permit
multiple controls with the same name (outside of radio
buttons). Also, you'd have to include a JavaScript
validation function in onClick for all of these that
would clear the others when one was checked. THe
browser does this for you with radios.





> >It might end up making the most sense to create a
> new tag for this purpose,
> and
> >leave the existing one as suitable for boolean
> properties.
> I agree with you. (for example
> <struts:checkboxgroup>
>
>
> Ralph
>
>

Just my $.02. Talk at you later

DC





__________________________________________________
Do You Yahoo!?
Yahoo! Photos -- now, 100 FREE prints!
http://photos.yahoo.com

Mime
View raw message