Thursday, July 21, 2005

Getting 'unchecked' value from Struts checkbox

The following was posted last year on a different blog. I'm consolidating posts here and you never know when this info may come in handy :)

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"/>
<html:hidden property="currentLocation" value="false"/>
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.
Works a treat.

39 comments:

Anonymous said...

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

Anonymous said...

I wasted around a day but this simple code has helped me a lot
Thanks
Khasim

Anonymous said...

Wonderful, thanks a lot man!

Anonymous said...

i tried many ways.But i didnt get any solution.it tried this one its woking excellent!!! Thanks man

White Pixie said...

Good tips ;)

Unknown said...

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!

Korhil said...

Thanks a lot, you are my hero.

Mark Dathorne said...

Bloody thing was driving me !INSANE! until this wonderful solution.
Thanks.
Mark

Paul Rivers said...

Thanks for posting this - it worked, so much easier than the other methods.

SyedAli said...

Thanks a lot man, It works!

Imtiaz Ali

SyedAli said...
This comment has been removed by the author.
El lado oscuro de java said...

Nice, this work!

Diego Montoya said...

Thanks a lot.

An ingenius solution

Peter Jerald said...

Excellent thinking. Thanks a lot :)

Anonymous said...

Still very useful after 7 years :) Thank you!

Birendra said...

Wonderful,it works.
Simple & Excellent thinking.

Anonymous said...

You are the best, beacouse you saved my life!!! :)

Anonymous said...

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.

Anonymous said...

Thank you, your solution helps me at last.

Anonymous said...

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

Anonymous said...

Thank you very very much

Anonymous said...

Thank you, this is still helping people!

kniveko said...

Cheers, this saved me a lot of time.

Anonymous said...

thanks a lot for sharing this, saved so much time!!!

Unknown said...

Excelenet post, it saved me a lot of time :-) Thanks.

Bala said...

Thanks saved my time

Bozyszcze said...

thx m8

Anonymous said...

Thanks so much for posting this tip! Saved me so much time trying to figure out a solution!
-Rema.

Muhammad Salman said...

Thank you so much man, saved my day.

Neo said...

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);
}

Mayank said...

Great Work!!

It saved our night @ office :-)

Anonymous said...

Thank you. After an hour of frustration you saved my day.

kalapur said...

Our best developers had spent 2 days to investigate this checkbox issue when we found your solution.
You helped us a lot! Great job!

Anonymous said...

it worked great for me.

Anonymous said...

I lost more than a day on that unsuccessful. Save me! Thank you!

Anonymous said...

Wow !! What an answer! I was just giving up when I saw this post... You save my day!

Amrin said...

Thanks alottttttttt buddy...I wasted one full day for it .

Adam said...

2017 and its still good

Unknown said...

Great,work appreciating