将列表绑定到弹簧中的复选框

时间:2014-09-06 06:22:25

标签: java spring data-binding checkbox converter

我有Offer实体类:

@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---->    
}

一个Category实体类     @实体     公共类别{

    public Category() {}

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

}

在我的offerDetails表单中,我试图将categoryList(属性Offer实体)绑定到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>

我有Offer实体类:

@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---->    
}

一个Category实体类     @实体     公共类别{

    public Category() {}

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

}

在我的offerDetails表单中,我试图将categoryList(属性Offer实体)绑定到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>

这是我的控制器:

@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;
}

转换器类:

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;
    }
}

在提交时,我希望选中复选框值以填充offerDetails.categoryList集合。

我也注册了转换器(用于将那些类别ID转换为categoryObjects)

Bean注册:

<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>

我仍然收到以下错误:

[无法将'java.lang.String'类型的属性值转换为属性'categoryList'所需的类型'java.util.List';嵌套异常是java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为属性'categoryList [0]'所需的类型[com.domain.Category]:找不到匹配的编辑器或转换策略]

我是Spring的新手。如果这是一个愚蠢的问题,请原谅。 非常感谢您的帮助。 :)

2 个答案:

答案 0 :(得分:0)

  1. 在JSP更改路径中引用类别
  2. 在类别实体中覆盖等于和哈希方法
  3. 转换器将Category实体中toString方法的结果转换为Category对象。

答案 1 :(得分:0)

您可以在控制器中使用@InitBinder解决,例如:

@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;
        }
    });
}