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.
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
ReplyDeleteI wasted around a day but this simple code has helped me a lot
ReplyDeleteThanks
Khasim
Wonderful, thanks a lot man!
ReplyDeletei tried many ways.But i didnt get any solution.it tried this one its woking excellent!!! Thanks man
ReplyDeleteGood tips ;)
ReplyDeleteArghhh... 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!
ReplyDeleteThanks a lot, you are my hero.
ReplyDeleteBloody thing was driving me !INSANE! until this wonderful solution.
ReplyDeleteThanks.
Mark
Thanks for posting this - it worked, so much easier than the other methods.
ReplyDeleteThanks a lot man, It works!
ReplyDeleteImtiaz Ali
This comment has been removed by the author.
ReplyDeleteNice, this work!
ReplyDeleteThanks a lot.
ReplyDeleteAn ingenius solution
Excellent thinking. Thanks a lot :)
ReplyDeleteStill very useful after 7 years :) Thank you!
ReplyDeleteWonderful,it works.
ReplyDeleteSimple & Excellent thinking.
You are the best, beacouse you saved my life!!! :)
ReplyDeleteThank 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.
ReplyDeleteThank you, your solution helps me at last.
ReplyDeleteHi
ReplyDeleteThis 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
ReplyDeleteThank you, this is still helping people!
ReplyDeleteCheers, this saved me a lot of time.
ReplyDeletethanks a lot for sharing this, saved so much time!!!
ReplyDeleteExcelenet post, it saved me a lot of time :-) Thanks.
ReplyDeleteThanks saved my time
ReplyDeletethx m8
ReplyDeleteThanks so much for posting this tip! Saved me so much time trying to figure out a solution!
ReplyDelete-Rema.
Thank you so much man, saved my day.
ReplyDeleteSorry 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 -
ReplyDeleteThe 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!!
ReplyDeleteIt saved our night @ office :-)
Thank you. After an hour of frustration you saved my day.
ReplyDeleteOur best developers had spent 2 days to investigate this checkbox issue when we found your solution.
ReplyDeleteYou helped us a lot! Great job!
it worked great for me.
ReplyDeleteI lost more than a day on that unsuccessful. Save me! Thank you!
ReplyDeleteWow !! What an answer! I was just giving up when I saw this post... You save my day!
ReplyDeleteThanks alottttttttt buddy...I wasted one full day for it .
ReplyDelete2017 and its still good
ReplyDeleteGreat,work appreciating
ReplyDelete