1
votes

I have Offer entity class :

@Entity
public class Offer {

    public Offer(){}
    private int offerId;
    private String offerBanner;
    private String offerLongDesc; 
    private int offerLikeCount ;

    List<Category> categoryList ;

    @DateTimeFormat(pattern= "dd/MM/yyyy")
    private Date offerStartDate; 
    <----all getters and setters---->    
}

And a Category entity class @Entity public class Category{

    public Category() {}

    private int categoryId;
    private int categoryParentId;
    private String categoryName;
    private Date categoryCreationDate;
    <--getters n setters--->

}

In my offerDetails form i am trying to bind the categoryList (attribute Offer entity) to checkbox

offerEntry.jsp

<form:form class="form-horizontal" action="/addOffer" commandName="offerDetails" method="post" enctype="multipart/form-data" style=" margin-top:20px; padding-right:50px; padding-left:10px; ">

    <div class="checkbox-list">
        <c:forEach var="category" varStatus="categoryStatus" items="${categoriesList}">
            <form:checkbox path="categoryList"  value="${category.categoryId}"/> <c:out                                                 value="${category.categoryName}" /><br>
        </c:forEach>
    </div>
    <-----Other div elements------>

</form:form>

I have Offer entity class :

@Entity
public class Offer {

    public Offer(){}
    private int offerId;
    private String offerBanner;
    private String offerLongDesc; 
    private int offerLikeCount ;

    List<Category> categoryList ;

    @DateTimeFormat(pattern= "dd/MM/yyyy")
    private Date offerStartDate; 
    <----all getters and setters---->    
}

And a Category entity class @Entity public class Category{

    public Category() {}

    private int categoryId;
    private int categoryParentId;
    private String categoryName;
    private Date categoryCreationDate;
    <--getters n setters--->

}

In my offerDetails form i am trying to bind the categoryList (attribute Offer entity) to checkbox

offerEntry.jsp

<form:form class="form-horizontal" action="/addOffer" commandName="offerDetails" method="post" enctype="multipart/form-data" style=" margin-top:20px; padding-right:50px; padding-left:10px; ">

    <div class="checkbox-list">
        <c:forEach var="category" varStatus="categoryStatus" items="${categoriesList}">
            <form:checkbox path="categoryList"  value="${category.categoryId}"/> <c:out                                                 value="${category.categoryName}" /><br>
        </c:forEach>
    </div>
    <-----Other div elements------>

</form:form>

and here is my controller :

@RequestMapping(value = {"/addOffer"}, method = RequestMethod.GET)
public ModelAndView offerForm() {

    ModelAndView mv = new ModelAndView("offerEntry");
    Offer offer = new Offer();
    try{
        List<Category> categoryList=categoryRepository.getAllCategories();
        mv.addObject("categoriesList",categoryList);
    }catch(Exception e)
    {
    }
    mv.addObject("offerDetails",offer);
    return mv;
}

Converter class :

public class CategoryIdtoCategory implements Converter<String, Category>
{
    @Inject
    private CategoryRepository categoryRepo;
    @Override
    public Category convert(String  categoryId)
    {
    try{
        int categoryIdI = Integer.parseInt(categoryId);
        return categoryRepo.findCategoryById(categoryIdI);
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

On Submit i want selected checkbox values to populate offerDetails.categoryList collection.

I have registered converter as well (for converting those category Ids to categoryObjects)

Bean registration:

<beans:bean id="conversionService"  class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
    <beans:property name="converters">
        <beans:list>
            <beans:bean class="com.service.util.CategoryIdtoCategory"/>
        </beans:list>
    </beans:property>
</beans:bean>

I am still getting following error :

[Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'categoryList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.domain.Category] for property 'categoryList[0]': no matching editors or conversion strategy found]

I am new to Spring . Please Excuse me if its a silly question. Your help would be appreciated . :)

2

2 Answers

0
votes
  1. In the JSP change path to reference the category
  2. In category entity override equal and hash methods
  3. In converter convert result of toString method in the Category entity to a Category object.
0
votes

You can solve with @InitBinder in your controller like :

@InitBinder
protected void initBinder(WebDataBinder binder) throws Exception{
    binder.registerCustomEditor(Set.class,"categoryList", new CustomCollectionEditor(Set.class){
        protected Object convertElement(Object element){
            if (element instanceof String) {
                Category category = categoryCache.get(Integer.parseInt(element.toString()));
                return role;
            }
            return null;
        }
    });
}