hi,

the workaround works, but causes problems if the checkbox should be
validated. More over the soln is based on assumptions that need not hold
always. There are 2 possible soln for this in my view:

1. use checkbox interceptor
(http://struts.apache.org/2.x/docs/checkbox-interceptor.html). But i found
that this doesnt work if u are using <s:checkboxList ..> the reson being
that the corresponding hidden text-field ("__checkbox_<checkboxListName>)" 
is not produced. (may be u can try creating this hidden field). Now use
checkbox interceptor before param interceptor of ur config xml file.

...
<interceptor-ref name="conversionError" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="params" /> 
...

2. Create a reset interceptor!: now what is this? Write a custom interceptor
(http://struts.apache.org/2.x/docs/writing-interceptors.html) which would
reset all the attributes/ field/ only chcek-box-field variables of the
action class. Place this interceptor as below: 
...
<interceptor-ref name="resetInterceptor">
  checkboxListvariable
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="params" />
...

Now what happens is: always before the paramas are loaded from the request
they are all reset to null/ what ever. Now if the params do not have the
checkbox field in it then the corresponding variables are not touched; this
way ur work is achieved. This would fire validations also. I am currently
using this approache and find it very useful. 

Hope this helps :)

cheers,
Ravi 



David Durham, Jr. wrote:
> 
>> >
>> >    request.getParameter("myUncheckedCheckbox") == null
>> >
>>
>> Oh.. yuck.. but thanks David for that work around.  Tried it and it
>> works for my purposes.
> 
> There are things that can be done to avoid to the ugly null check.
> 
> 1. Don't initialize check-box fields.  No initial values.
> 2. Don't use session-scoped forms.
> 
> It sounds like you need to start with a blank or default form that has
> a check-box checked.  Perhaps the user should receive spam?  :)  I
> haven't thought about that case much, but it seems similar in that, if
> you don't want to use an initial value for reasons previously
> mentioned, you're faced with using some other means to indicate this
> box is checked by default.  If you have a "display new form action,"
> you can put a value on the form whereby the box will start out
> checked.  This is different than defining an initial value.   So long
> as you don't specify an initial value for check-box fields, and Struts
> creates a new form object for each submission, the state of a
> check-box is preserved within an ActionForm.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/unexpected-behavior-with-a-checkbox.-tp14572206p14591715.html
Sent from the Struts - User mailing list archive at Nabble.com.


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

Reply via email to