【发布时间】:2012-09-09 12:21:37
【问题描述】:
我不确定这是否是一个复杂的问题,但作为一个初学者,这对我来说似乎有点复杂。 我有一个基于它的对象,我需要在 UI 上显示一些值并让用户选择其中一些值,当用户单击提交按钮时,我需要将数据发送回另一个控制器。这是我的数据对象的结构
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 表达式不正确
【问题讨论】:
-
在这种情况下要注意的另一件事是嵌套类。将
PrsCDData设为顶级类可以避免这个问题。
标签: java jsp spring-mvc jstl