Announcement Announcement Module
No announcement yet.
form:checkbox tag, binding a Collection of a custom type Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • form:checkbox tag, binding a Collection of a custom type

    I have a command object (ProductCO) that has a property of custom type, my ProductCategory class.

    public class ProductCO {
        protected Collection<ProductCategory> categories;
        /* ... other fields */
        public Collection<ProductCategory> getCategories() {
            return categories;
        public void setCategories( Collection<ProductCategory> categories ) {
            this.categories = categories;
        /* ... other getters and setters */
    I use a custom Property editor to let Spring handle the conversions between String and ProductCategory.

    public class ProductCategoryTypeEditor extends PropertyEditorSupport {
        protected ProductCategoryService productCategoryService;
        public ProductCategoryTypeEditor(ProductCategoryService productCategoryService) {
            this.productCategoryService = productCategoryService;
        public void setAsText(String text) throws IllegalArgumentException {
            Long productCategoryID = Long.parseLong(text);
            ProductCategory productCategory = productCategoryService.getProductCategoryById(productCategoryID);
    But now I would like to use this command object with form:checkbox tag, like this:

    <form:form method="POST" commandName="product">
            <c:forEach items="${categoryList}" var="category">
                    <form:checkbox path="categories" value="${}"/>                   
    According to the Spring documentation, I need a property of the same type as the bound value, which is a Collection of Longs ( is Long) in my command object. The code above doesn't work as the categories property is a Collection of ProductCategory types.

    Approach Two - When the bound value is of type array or java.util.Collection, the input(checkbox) is marked as 'checked' if the configured setValue(Object) value is present in the bound Collection.

    I could add new property (something like categoryIds) of type Long in the ProductCO command object, but then I would have to handle conversions between Strings and ProductCategory classes manually, which is now handled by the ProductCategoryTypeEditor.

    Is there any way how to achieve this with using collections of properties with custom types? Any tips are very welcome.


  • #2
    I use Map for checkbox's list.

    Map check = new HashMap();
    for (Iteraror ... )
    check.put(, Boolean.FALSE);