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:

  1. 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

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

    ReplyDelete
  3. Wonderful, thanks a lot man!

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

    ReplyDelete
  5. 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!

    ReplyDelete
  6. Thanks a lot, you are my hero.

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

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

    ReplyDelete
  9. Thanks a lot man, It works!

    Imtiaz Ali

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Thanks a lot.

    An ingenius solution

    ReplyDelete
  12. Excellent thinking. Thanks a lot :)

    ReplyDelete
  13. Still very useful after 7 years :) Thank you!

    ReplyDelete
  14. Wonderful,it works.
    Simple & Excellent thinking.

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

    ReplyDelete
  16. 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.

    ReplyDelete
  17. Thank you, your solution helps me at last.

    ReplyDelete
  18. 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

    ReplyDelete
  19. Thank you very very much

    ReplyDelete
  20. Thank you, this is still helping people!

    ReplyDelete
  21. Cheers, this saved me a lot of time.

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

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

    ReplyDelete
  24. Thanks saved my time

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

    ReplyDelete
  26. Thank you so much man, saved my day.

    ReplyDelete
  27. 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);
    }

    ReplyDelete
  28. Great Work!!

    It saved our night @ office :-)

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

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

    ReplyDelete
  31. it worked great for me.

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

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

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

    ReplyDelete
  35. 2017 and its still good

    ReplyDelete