【问题标题】:My Spring CustomCollectionEditor implementation , not binding correctly我的 Spring CustomCollectionEditor 实现,未正确绑定
【发布时间】:2011-03-31 22:03:47
【问题描述】:

我在春天有一个选择多个:

<form:select  path="categoryProducts" id="destinationData" itemLabel="product.name" items="${categoryProducts}" itemValue="product.id" multiple="true" ondblclick="moveLeft(document.getElementById('destinationData'), document.getElementById('sourceData'))"  size="10" />  

在我的控制器中:

@InitBinder
  public void initBinder(WebDataBinder binder){
    binder.registerCustomEditor(Set.class, "categoryProducts", new CustomCollectionEditor(Set.class)
    {
      @Override
      protected Object convertElement(Object element)
      {
          Product  p = new Product() ;
         try {

             Short id = new Short(String.valueOf(element));
              p = (Product) dataManager.find(Product.class, id);
             System.out.println(p.getId() + "\\" +p.getName());
             CategoryProduct c = new CategoryProduct ();
             c.setProduct(p);
             return c;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return p;
      }


    });  

当我提交时,收到此日志:

136 Product Name
105 Product Name
104 Product Name
211 Product Name
204 Product Name
409 Product Name
30/03/2011 07:32:20 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet cms lanzó excepción
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'category' on field 'categoryProducts': rejected value [136,105,104,211,204,409][type

initBinder 被初始化,但最后抛出异常: (rejected value [136,105,104,211,204,409]) 。 为什么?

【问题讨论】:

    标签: spring data-binding


    【解决方案1】:

    您的命令类(模型属性)的字段“categoryProducts”很可能是一个

    Set<String> 
    

    ...或除 CategoryProduct 之外的任何类的 Set。尝试将其更改为:

    Set<CategoryProduct>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2013-12-05
      • 2016-12-28
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多