Unable to bind data using spring data function

Thread continued by sending-data-back-to-controller-spring-mvc

I am working on a product details page where I need to show the user some parameters, and the user will select several of them, and on the product the submit button should be added to the basket. My intentions are to pass this Data object to my basket controller so that I can use these values, and since the object contains dynamic values, it is therefore impossible to define an object of predefined fields. This is my data object.

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 } 

In my product detail page controller, I set the values ​​as:

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

and on my JSP product details page I have this in my form:

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

But when I click the submit button, I get the following exception

 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 [ com.product.data.PrsCDData@6164f07e ] 

I am not sure where I am doing it wrong, because on the product details page these hidden fields are correctly linked and even have the values ​​assigned to them, but when the form is submitted, I have to deal with this problem.

+1
source share
1 answer

LazyMap factory should return a LazyList.

this factory FactoryUtils.instantiateFactory (PrsCDData.class) creates a new PrsCDData strong> object , not a PrsCDData list.

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

it should be

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

use LazyList as you want to access index '0' right away, which leads to ArrayIndexOutOfBoundsExecption otherwise

EDIT: A Simple Example

 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; } } 
+1
source

All Articles