Results 1 to 4 of 4

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

  1. #1

    Question <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. #2
    Join Date
    Mar 2009
    Location
    Bangalore,India
    Posts
    35

    Default

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

  3. #3
    Join Date
    Nov 2006
    Location
    Columbus, OH
    Posts
    143

    Default

    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:

    Code:
    public class MySimpleFormController extends SimpleFormController {
        ...
        @Override
        protected Object formBackingObject(HttpServletRequest request)
                throws Exception {
            MyFormCommand command = new MyFormCommand();
            ...
            command.setBooleanValue(true);
            ...
            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. #4
    Join Date
    Mar 2009
    Location
    Bangalore,India
    Posts
    35

    Default

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

    Cheers,
    Amit

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •