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

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

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

    Code:
    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.

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

    Code:
    <form:form method="POST" commandName="product">
        <ul>
            <c:forEach items="${categoryList}" var="category">
                <li>
                    ${category.name}
                    <form:checkbox path="categories" value="${category.id}"/>                   
                </li>
            </c:forEach>
        </ul>
    </form:form>
    According to the Spring documentation, I need a property of the same type as the bound value, which is a Collection of Longs (category.id 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.

    Vladimir

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

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

    Comment

    Working...
    X