Alright!
I finally figured out a way around this.  I'm all proud cuz this has been
pissing me off for a while now.  If you don't use the reset method, then you
just have to put one tag after your checkbox.  AFTER the checkbox tag, put
<jsp:setProperty name="beanName" property="prop" value="false" />.  This
will let the checkbox load normally, then set the property to false after
it's loaded, so now all required properties will be correctly updated.  Try
it out, it works (at least for me).  Is this workaround frowned upon by the
struts gurus?

~ Keith
http://www.buffalo.edu/~kkamholz


-----Original Message-----
From: Shaun Whyte [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 30, 2002 9:39 AM
To: Struts Users Mailing List
Subject: Re: reflect the uncheck in the bean


Giles

This is a similar problem to what I have been having.  The only way around I
have found is to use a seperate boolean attribute which will store the
"real" value of the checkbox, the other is simply an initial value.  For
example if I have a check box called "test", then:

===============================
// Declare checkbox attributes
private boolean testInitial;
private boolean test;

// One setter method - in it, set
public void setTestInitial(boolean newValue) {
    test = newValue
}

// Two getter methods, one used by form (testInitial), other used by actions
public boolean getTestInitial() {
    return testInitial;
}

public boolean getTest() {
    return test;
}

// Reset method, always set test to false, set testInitial to whatever you
want
public void reset() {
    testInitial = true;
    test = false;
}
================================

And in your form, we should have:
<html:checkbox property="testInitial"/>
===============================

Your action class should use the getTest() method to get the actual value of
the checkbox submitted.  It will appear on the form as checked, but the
value actually being used is false.  If it is left checked, it will call
getTestInitial() which will set the real value (test) to true, if not, it
will remain false.

I asked a question yesterday about the fact that if the validation fails,
then it doesn't show the actual values of the checkbox when the form
re-appears to show the error messages, since testInitial is always true.
Although its not a big issue, does anyone know a way around this?

Hope this helps anyhow!

Shaun.



----- Original Message -----
From: <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Sent: Tuesday, July 30, 2002 2:22 PM
Subject: RE: reflect the uncheck in the bean


>
> Hi Andrew,
> My form bean have to be populated whith data retrieved from a DB.
> My bean is generic, and have to recieve any array of values; so I use
> String types for all properties.
> If the property corresponding to my checkbox is set to true when
retrieving
> data, the jsp will generate a checked checkbox. Right?
> If I uncheck the  checkbox in my browser, the setter of my been will not
be
> fired and the corresponding property will leave unchanged. Right?
> If I'm right till there, I don't understand how will my bean reflect the
> unchecking of the checkbox.
> I agree whith the use of the reset() to set the good value when
> initializing the bean, but how does it work if the default have to be
> 'true'?
>
> You said:
> In the reset method of the form, set the field for which the checkbox
> applies to false (or other appropriate internal representation...). That
> way
> if you uncheck the box (and html submits nothing), then it will become
> false
> in the form...
> When do i need to use the reset() in your opinion? Is it automaticaly
fired
> by Struts?How will it 'become' false
> in the form ? For my use case, I will use a restore() who will be fired
> once at the retrieve, before generating the jsp.
>
> Tx for your help.
>
> Gilles Vandaele
>
>
>
>                        Andrew Hill
>                        <andrew.david.hill@g To:    Struts Users Mailing
List
>                        ridnode.com>
<[EMAIL PROTECTED]>
>                                             cc:
>                        30/07/2002 12:28     Subject:  RE: reflect the
uncheck in the bean
>
>                                             Please respond to Struts Users
Mailing List
>
>
>
>
>
>
>
> Yep.
>
> In the reset method of the form, set the field for which the checkbox
> applies to false (or other appropriate internal representation...). That
> way
> if you uncheck the box (and html submits nothing), then it will become
> false
> in the form, but if you ticked it on the screen than html will submit a
> value, and this will get populated into your form as normal (overriding
the
> change you just made in reset()).
>
> I didnt realise nothing was submitted for an empty text field! I though it
> submitted an empty string as the value for those? You sure about that one?
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, July 30, 2002 17:22
> To: Struts Users Mailing List
> Subject: reflect the uncheck in the bean
>
>
>
> Hi everybody,
>
> When I uncheck a checkbox in an HTML form, the pair name/value don't
appear
> in the request, and the ActionForm bean cannot reflect this.
> It's the same for a text input who's left empty.
>
> Is there a workaround using Struts?
>
>
>
> Gilles Vandaele
>
>
> --
> To unsubscribe, e-mail:
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
>
>
> --
> To unsubscribe, e-mail:   <
> mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <
> mailto:[EMAIL PROTECTED]>
>
>
>
>
>
>
> --
> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
>
>
>


--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to