• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts : checkbox status is unchanged if i use back button and uncheck it

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I have a couple of checkboxes in a jsp page used in struts framework. I am using DynaValidatorForm as form bean with session scope, so it means the properties are mapped only in the struts-config.xml. the checkboxes' property names are checkbox1 and checkbox2.
now the in the action class, i do:

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {DynaValidatorForm dynaform = (DynaValidatorForm)form; System.out.println("1st checkbox value:"+ dynaform.get("checkbox1") ); System.out.println("2nd checkbox value:"+ dynaform.get("checkbox2") ); .....}


Now if I check the first checkbox and press on submit button, the action class prints out:

1st checkbox value: on
2nd checkbox value: off

this is as expected

Now I click on the back button, then i uncheck the first checkbox and click on submit.
the output is same as before, i.e.:
1st checkbox value: on //wrong
2nd checkbox value: off

I expected it to be
1st checkbox value: off
2nd checkbox value: off

That means if in a session if i check on checkbox 'on', and i go back and uncheck it, the uncheck is never stored, i.e. the property is never set to 'off' .
Now if i check the second one, and click on submit, output is :
1st checkbox value: on //expected off
2nd checkbox value: on //correct

now i go back and uncheck the 2nd checkbox (the first checkbox is already unchecked before), click submit and i get this output:

1st checkbox value: on //expected off
2nd checkbox value: on //expected off

Please let me know what is happening. I expect the values of uncheck boxes to be 'off' when i go back and uncheck them.
thanks
Tanveer
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
i got one solution, i.e to use a array of string in the dyna action form definition in struts-config.xm:

<form-property name="companies" type="java.lang.String[]"/>

then i write the jsp as:

<html:checkbox property="companies" value="CanadaLife"/>CanadaLife<br>
<html:checkbox property="companies" value="Eagle Star"/>Eagle Star<br>
<html:checkbox property="companies" value="Hibernian"/>Hibernian<br>
<html:checkbox property="companies" value="Irish Life"/>Irish Life<br>
<html:checkbox property="companies" value="New Ireland"/>New Ireland<br>

this way i will get an array of String when i do ((DynaValidateForm)form).get("companies");
I will have ti iterate thru the array and compare their values to know which ones are selected(i.e. which ones are in the array). Those not in the array means they were not selected
 
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic