Announcement Announcement Module
No announcement yet.
<form:checkbox/> how to have checked by default Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • <form:checkbox/> how to have checked by default

    I have List of objects of MyClass.

    MyClass has "equals" method that works correct. Also in my controller I have CustomerCollectionEditor that coverts string value of object MyClass and returns object of MyClass.

    So when I use this technology data submits and returns correctly List of elements according to view, but I have problem when validation don't pass in controller. This time some of checkboxes must be checked, but all are not checked.

    I tried same with <form:select multiple="true"/> here all works perfect, but in case of checkboxes this don't work.

    Could you support or give short example like mine?

  • #2
    m having same problem here too..Started a thread also.But no answers yet..


    • #3
      Amit, et al.

      I'm going to assume you're extending the SimpleFormController class. I'm not sure you can set the default value with the <form:checkbox path="booleanValue" /> JSP tag. Historically speaking, I've found it best to override the protected Object formBackingObject(HttpServletRequest request) method to setup the form object to be used before actually rendering the form. If you do not override this method, the command object that gets returned is effectively new MyCommandObject() meaning the default values are taken from the default constructor of your command object.

      To set the default value, you should properly initialize your form backing object and set the appropriate default values. This should look something like the following snippet:

      public class MySimpleFormController extends SimpleFormController {
          protected Object formBackingObject(HttpServletRequest request)
                  throws Exception {
              MyFormCommand command = new MyFormCommand();
              return command;
      When the form gets rendered, the booleanValue is true which results in the checkbox being checked by default.

      Alternatively, you can set default values in the default constructor of the command object; although I do not recommend this approach as each form controller may have different requirements for the default values of a command object. Going this route often discourages good code reuse techniques.

      Hope that helps,

      Joshua Preston.


      • #4
        Hey Joshua,
        Thanks a lot.that solved my problem in almost 30 seconds.Its precisely what i wanted.Thank u again.