I am not sure if this is a complex problem but as a starting person this seems a bit complex to me.
I have an object based on which i need to show some values on the UI and let user select some of them, i need to send data back to another controller when user click on the submit button.Here is the structure of my data object
public class PrsData{
private Map<String, List<PrsCDData>> prsCDData;
}
public class PrsCDData{
private Map<String, Collection<ConfiguredDesignData>> configuredDesignData;
}
public ConfiguredDesignData{
// simple fields
}
I have set the object in model before showing the view like
model.addAttribute("prsData", productData.getPrData());
In the form i have following settings
<form:form method="post" commandName="prsData" action="${addProductToCartAction}" >
<form:hidden path="prsCDData['${prsCDDataMap.key}']
[${status.index}].configuredDesignData['${configuredDesignDataMap.key}']
[${configuredDesignDataStatus.index}].code"/>
<form:hidden path="prsCDData['${prsCDDataMap.key}']
[${status.index}].configuredDesignData['${configuredDesignDataMap.key}']
[${configuredDesignDataStatus.index}].description"/>
</form:form>
This is what i have at AddProductToCartController
public String addToCart(@RequestParam("productCodePost") final String code,
@ModelAttribute("prsData") final PrsData prsData, final Model model,
@RequestParam(value = "qty", required = false, defaultValue = "1") final long qty)
On submitting the form i am getting following exception
org.springframework.beans.NullValueInNestedPathException: Invalid property 'prsCDData[Forced][0]'
of bean class [com.product.data.PrsData]:
Cannot access indexed value of property referenced in indexed property path 'prsCDData[Forced][0]': returned null
It seems like its trying to access the values on this controller while i am trying to send value to that controller and trying to create same object with selected values
Can any one tell me where i am doing wrong and what i need to take care of
Edit
I did some more research and came to know that Spring do not support auto-populating list/map for custom objects and based on the answer i tried to change implementation like
public class PrsData{
private Map<String, List<PrsCDData>> prsCDData;
// lazy init
public PrsData()
{
this.prsCDData = MapUtils.lazyMap(new HashMap<String, List<PrsCDData>>(),
FactoryUtils.instantiateFactory(PrsCDData.class));
}
}
public class PrsCDData{
private Map<String, Collection<ConfiguredDesignData>> configuredDesignData;
public PrsCDData()
{
this.configuredDesignData = MapUtils.lazyMap(new HashMap<String,
List<ConfiguredDesignData>>(),
FactoryUtils.instantiateFactory(ConfiguredDesignData.class));
}
}
but i am getting following exception
org.springframework.beans.InvalidPropertyException:
Invalid property 'prsCDData[Forced][0]' of bean class [com.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.data.PrsCDData@6043a24d]
i am not sure which thing i am doing wrong, it seems that either my JSTL expression is not right
See Question&Answers more detail:
os