【问题标题】:Binding list to checkbox in spring春季绑定列表到复选框
【发布时间】:2014-10-31 02:13:37
【问题描述】:

我有 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 实体)绑定到复选框

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 实体)绑定到复选框

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 新手。如果这是一个愚蠢的问题,请原谅。 您的帮助将不胜感激。 :)

【问题讨论】:

    标签: java spring data-binding checkbox converter


    【解决方案1】:
    1. 在JSP改变路径中引用类
    2. 在类别实体中覆盖相等和哈希方法
    3. 在转换器中,将 Category 实体中的 toString 方法的结果转换为 Category 对象。

    【讨论】:

      【解决方案2】:

      您可以在控制器中使用@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;
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-03
        • 1970-01-01
        • 2017-04-14
        • 2018-01-03
        相关资源
        最近更新 更多