Get your CodeRanch badge!*
The moose likes Struts and the fly likes Struts: checkbox's previous value remains if i go back and unselect it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: checkbox Watch "Struts: checkbox New topic
Author

Struts: checkbox's previous value remains if i go back and unselect it

Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
hi
I have a checkbox in a JSP in struts environment:

<html:checkbox property="conversionOption"/>


now in struts-config, the dynaactionform is :

now if the checkbox is not clicked, and in the action class I do:

String s = (String)formbean.get("conversionOption");

s is null.
Now when i check(select) the check box, and click on submit the String s is 'on'. After this if I click on 'back' button and then uncheck the checkbox and click submit., the String is still 'on'. I expect it to be null or something other than 'on'.
It seems struts does not set conversionOption property if the checkbox for that property is not selected. so the previous value remains.
how do I overcome this?
I tried by changing :
<form-property name="conversionOption" type="java.lang.String"/>
to
<form-property name="conversionOption" type="java.lang.String[]"/>
but it did not help. changing to String[] will help if i have musltiple checkboxes for the same property. but my form has only one checkbox.
can anybody please help me?
Tanveer

[ edited to break long code line -ds ]
[ September 02, 2004: Message edited by: Dirk Schreckmann ]

Author of JPhotoBrush Pro (www.jphotobrushpro.com)
Rakesh Sahadevan
Greenhorn

Joined: Jul 11, 2004
Posts: 12
Do u use browsers back button
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Web Application Frameworks forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Hi Tanveer,

Try using the reset() method on the Form bean class. It can be used to reset/reinitialize your form bean variables.

Sheldon
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Hi tanveer

Thats exactly what happens ......................thats the behaviour of checkbox rather than a tag and let me tell u one thing if the scope of the for m bean is session only it will happen ...........then the option for this is to use reset method was what he said

but if the bean is in request scope then this wont happen
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Hi Tanveer,

The explanation for this behaviour is quite simple.

When a form is submitted, only "on" checkbox controls can become successful (A successful control is "valid" for submission). Hence, if a checkbox has not been checked, the framework does not populate the form attribute bound to this control (no request parameter was submitted). The previous value of the attribute persists if the scope of the form bean is "session". This problem is not encountered when using request scope form beans since there is no previous value (unless you explicitly set one).

The solution is to use the reset() method of the form bean. If the form bean is in the session, it's important to implement the form's reset method to initialize the form before each use.

Sheldon Fernandes :roll:
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
Thanks for the responses. In response to your questions:
Actually this happens when i press the back button.
Also the form bean has session scope.
Usually the same form does not appear again in the flow, but the user may click on 'back button' to go back to the form and uncheck the checkbox.
I do not want to reset the whole form,but what I think I can do is in the action class that is invoked when i click submit for this form, I can set the property to null (kind of reseting a particular property). Is this okay?
[ September 02, 2004: Message edited by: Tanveer Rameez ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts: checkbox's previous value remains if i go back and unselect it
 
Similar Threads
How to get results from indexed fields????
Error while trying NewsLetter Example
Struts 1.1 validation help on Requiredif
I am getting error when starting Tomcat .How can I rectify it?
Cannot Retrieve Definition for Form Bean Null