gpt4 book ai didi

java - 无法使用 Spring Data 功能绑定(bind)数据

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:07 24 4
gpt4 key购买 nike

线程是 sending-data-back-to-controller-spring-mvc 的延续

我正在开发一个产品详细信息页面,我需要向用户展示一些选项,用户将选择其中的一些选项,并且在提交按钮上产品应添加到购物篮中。我的意图是将该数据对象传输到我的购物车 Controller ,以便我可以使用这些值,并且由于该对象包含动态值,因此不可能定义预先确定的字段对象。这是我的数据对象

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
}

在我的产品详细信息页面 Controller 中,我将值设置为:

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

我不确定我在哪里做错了,因为在产品详细信息页面上,这些隐藏字段被正确绑定(bind),甚至为它们分配了值,但是当提交表单时,我面临这个问题。

最佳答案

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;
}

}

关于java - 无法使用 Spring Data 功能绑定(bind)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12348491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com