在Spring MVC中绑定列表映射
问题内容:
我不确定这是否是一个复杂的问题,但是作为一个开始的人,这对我来说似乎有点复杂。我有一个基于它的对象,我需要在UI上显示一些值并让用户选择其中的一些值,当用户单击Submit按钮时,我需要将数据发送回另一个控制器。这是我的数据对象的结构
public class PrsData{
private Map<String, List<PrsCDData>> prsCDData;
}
public class PrsCDData{
private Map<String, Collection<ConfiguredDesignData>> configuredDesignData;
}
public ConfiguredDesignData{
// simple fields
}
在显示如下视图之前,我已经在模型中设置了对象
model.addAttribute("prsData", productData.getPrData());
在表格中,我有以下设置
<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>
这就是我所拥有的 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)
在提交表格时,我得到以下异常
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
似乎它试图访问此控制器上的值,而我正在尝试将值发送到该控制器并尝试创建具有选定值的相同对象
谁能告诉我我在哪里做错了以及我需要照顾什么
编辑
我做了一些进一步的研究,得知Spring不支持为自定义对象自动填充列表/地图,并且基于我试图更改实现的答案
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));
}
}
但是我正在遵循异常
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]
我不确定我做错了什么,似乎我的JSTL表达式不正确
问题答案:
说明: 如果在您的控制器中有@ModelAttribute("user") User user
,并且您加载了一个包含的相应页面,则将<form:form commandName="user">
实例化一个空用户。
其所有属性均为null,如果是List或Map,则为空。此外,其空列表/映射已通过自动增长的实现实例化。这是什么意思
?假设我们有一个空的自动增长List<Coconut>coconuts
。如果我这样做coconuts.get(someIndex).setDiameter(50)
,它将工作而不是引发异常,因为列表自动增长并为给定索引实例化一个椰子。
由于这种自动增长,使用以下输入内容提交表单将非常有用:
<form:input path="coconuts[${someIndex}].diameter" />
现在回到您的问题: Spring
MVC自动增长可以很好地与一系列对象配合使用,每个对象都包含一个地图/列表。但是给定您的例外,看来Spring不会自动增长自动增长的列表/地图所包含的可能对象。在中Map<String,List<PrsCDData>> prsCDData
,List<PrsCDData>
只是一个没有自动增长的空列表,因此导致您的异常。
因此,解决方案必须使用某种Apache
Common的LazyList或Spring的AutoPopulatingList。
您必须实现自己的自动增长映射,该映射实例化给定索引的LazyList / AutoPopulatingList。从头开始或使用某种Apache
Common的LazyMap
/
MapUtils.lazyMap实现(到目前为止,我还没有找到与LazyMap等效的Spring)。
使用Apache Commons Collections的解决方案示例:
public class PrsData {
private Map<String, List<PrsCDData>> prsCDData;
public PrsData() {
this.prsCDData = MapUtils.lazyMap(new HashMap<String,List<Object>>(), new Factory() {
public Object create() {
return LazyList.decorate(new ArrayList<PrsCDData>(),
FactoryUtils.instantiateFactory(PrsCDData.class));
}
});
}
}