Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rakesh Sahadevan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do u use browsers back button
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Web Application Frameworks forum...
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic