Having just spent half the afternoon trying to get a page with a checkbox to work correctly with Struts I stumbled across the following. I knew that an unchecked value was never returned in the form and, as we are using Struts DynaActionForms, was starting to think about over-riding the Form when I came across a solution that is so simple it's embarrassing.
The solution is to add a hidden field immediately after the checkbox field i.e.
<html:checkbox property="currentLocation" value="true"/>When the form is submitted if the checkbox is selected the hidden field is ignored, but if the checkbox is empty it isn't in the form so the hidden field is used.
<html:hidden property="currentLocation" value="false"/>
Works a treat.
39 comments:
Thanks for excellent code snippet. I was wondering to use reset method on view bean but this worked and saved half a day for me
I wasted around a day but this simple code has helped me a lot
Thanks
Khasim
Wonderful, thanks a lot man!
i tried many ways.But i didnt get any solution.it tried this one its woking excellent!!! Thanks man
Good tips ;)
Arghhh... such an easy solution, can't believe I didn't come up with this myself about 4 hours ago... thanks for the help... it works great!
Thanks a lot, you are my hero.
Bloody thing was driving me !INSANE! until this wonderful solution.
Thanks.
Mark
Thanks for posting this - it worked, so much easier than the other methods.
Thanks a lot man, It works!
Imtiaz Ali
Nice, this work!
Thanks a lot.
An ingenius solution
Excellent thinking. Thanks a lot :)
Still very useful after 7 years :) Thank you!
Wonderful,it works.
Simple & Excellent thinking.
You are the best, beacouse you saved my life!!! :)
Thank you, thank you, thank you!! Found this solution by fluke. Been struggling with this for 2 days. Why this isn't in any of the struts examples I have no idea.
Thank you, your solution helps me at last.
Hi
This is not the right solution.
The ordersin which parameters are sent to the server depend on the browser so:
is not the same as
The right solution is to set the boolean value in form action form in the reset method.
public void reset(ActionMapping mapping, HttpServletRequest request) {
setCurrentLocation(false);
}
Like this the boolean value is false by default.
If the checkbox is checked, the value will be change to true
Regards
Thank you very very much
Thank you, this is still helping people!
Cheers, this saved me a lot of time.
thanks a lot for sharing this, saved so much time!!!
Excelenet post, it saved me a lot of time :-) Thanks.
Thanks saved my time
thx m8
Thanks so much for posting this tip! Saved me so much time trying to figure out a solution!
-Rema.
Thank you so much man, saved my day.
Sorry to say this, but this post is not quite recommended practice according to Struts mannual which says that Boolean type value should be used with tag. So, the guy who recommended the following solution is good and should be preferred instead of the original post -
The right solution is to set the boolean value in form action form in the reset method.
public void reset(ActionMapping mapping, HttpServletRequest request) {
setCurrentLocation(false);
}
Great Work!!
It saved our night @ office :-)
Thank you. After an hour of frustration you saved my day.
Our best developers had spent 2 days to investigate this checkbox issue when we found your solution.
You helped us a lot! Great job!
it worked great for me.
I lost more than a day on that unsuccessful. Save me! Thank you!
Wow !! What an answer! I was just giving up when I saw this post... You save my day!
Thanks alottttttttt buddy...I wasted one full day for it .
2017 and its still good
Great,work appreciating
Post a Comment