2012-09-10 58 views
1

線程處於sending-data-back-to-controller-spring-mvc無法使用彈簧數據功能

我的工作,我需要展示用戶產品詳細信息頁上的延續將數據綁定一些選項和用戶將選擇少數人,並提交按鈕產品應該添加到籃子裏。 我的意圖是將該數據對象轉移到我的購物車控制器,以便我可以使用這些值,並且由於該對象包含動態值,因此無法定義預先確定的字段對象。 這是我的數據對象

public class PrsData { 
    private Map<String, List<PrsCDData>> prsCDData; 

    public PrsData(){ 
     this.prsCDData = MapUtils.lazyMap(new HashMap<String, List<PrsCDData>>(), 
     FactoryUtils.instantiateFactory(PrsCDData.class)); 
    } 
} 

public class PrsCDData { 
    private Map<String, List<ConfiguredDesignData>> configuredDesignData; 
    // same lazy map initialization 
} 

在我的商品詳細頁控制器,我設定值:

model.addAttribute("prsData", productData.getPrsData()); 

,我的商品詳細頁上的JSP我有這個在我的表格:

<form:form method="post" commandName="prsData" action="${addProductToCartAction}" > 
    <form:hidden path="prsCDData[''${prsCDDataMap.key}''] 
        [${status.index}].configuredDesignData['${configuredDesignDataMap.key}'] 
        [${configuredDesignDataStatus.index}].code" /> 
</form:form> 

但是當我點擊提交按鈕,我收到以下異常

org.springframework.beans.InvalidPropertyException: 
    Invalid property 'prsCDData['Forced'][0]' of bean class [com.product.data.PrsData]: 
     Property referenced in indexed property path 'prsCDData['Forced'][0]' 
     is neither an array nor a List nor a Set nor a Map; 
     returned value was [[email protected]] 

我不知道我在做什麼錯誤,因爲在產品詳細信息頁面上這些隱藏的字段綁定正確,甚至有值分配給他們,但當表單被提交時,我面臨這個問題。

回答

1

LazyMap工廠必須返回一個LazyList。

給定的工廠FactoryUtils.instantiateFactory(PrsCDData.class)創建一個新的PrsCDData對象,而不是PrsCDData的名單。

prsCDData['Forced'] -> if exists then return it else create instance of PrsCDData.class 

應該

prsCDData['Forced'] -> if exists then return it else create instance of LazyList<PrsCDData> 

使用LazyList因爲你馬上要訪問索引 '0' 是什麼導致了ArrayIndexOutOfBoundsExecption否則

編輯:簡單的例子

public class Test { 

@SuppressWarnings("unchecked") 
public static void main(String[] args) throws UnsupportedEncodingException { 

    Map<String, List<SimpleBean>> map = MapUtils.lazyMap(new HashMap<String,List<Object>>(),new Factory() { 

     public Object create() { 
      return LazyList.decorate(new ArrayList<SimpleBean>(), FactoryUtils.instantiateFactory(SimpleBean.class)); 
     } 
    }); 

    System.out.println(map.get("test").get(0)); 
} 

public static class SimpleBean { 
    private String name; 
} 

} 
+0

+1真的很好發現錯誤,會嘗試這一點,並會更新,但對我來說,這似乎是99%的問題 –