hi I have a checkbox in a JSP in struts environment:
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)
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
Joined: Aug 18, 2004
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:
Joined: Dec 11, 2000
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 ]
subject: Struts: checkbox's previous value remains if i go back and unselect it