Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 在Spring中将组合对象用作RequestParam的最简单方法_Spring Mvc - Fatal编程技术网

Spring mvc 在Spring中将组合对象用作RequestParam的最简单方法

Spring mvc 在Spring中将组合对象用作RequestParam的最简单方法,spring-mvc,Spring Mvc,下面的内容有点像。。。伪代码。。。为了说明我在寻找什么: //此处使用setter和getter来简化示例: 班级地址 { 私家弦街;; 私人城市; } 类AddressBookEntry { 私有字符串名称; 私人地址; } 类MyController { 公共void呈现(@RenderParam AddressBookEntry) { ... } } 如您所见,有两个POJO(Address和AddressBookEntry)。现在,我想将AddressBookEntry作为http请求

下面的内容有点像。。。伪代码。。。为了说明我在寻找什么:

//此处使用setter和getter来简化示例:
班级地址
{
私家弦街;;
私人城市;
}
类AddressBookEntry
{
私有字符串名称;
私人地址;
}
类MyController
{
公共void呈现(@RenderParam AddressBookEntry)
{
...
}
}
如您所见,有两个POJO(Address和AddressBookEntry)。现在,我想将AddressBookEntry作为http请求参数传递给我的控制器。我想象URL是这样的:
/target?entry.name=Random Guy&entry.address.street=Random street&entry.address.city=Random city

据我所知,@RenderParam不是这样工作的。我必须创建一个PropertyEditor,它接受一个字符串并从中构造我的目标对象,这意味着我不能为每个(子)属性拥有一个单独的URL参数

@ModelAttribute更接近于此,但我找不到任何关于对象嵌套是否以及如何与此注释一起工作的提示。此外,此注释在上面我的URL中没有“entry.”前缀,这意味着我需要确保没有多个ModelAttributes共享一个属性名,对吗?听起来很有压力


如何解决这个问题?

在这种情况下,您应该使用
@modeldattribute
。它支持您想要的嵌套对象


如果您需要多个
@modeldattribute
s,您可以将它们组合到一个特殊的类中(例如,如果您认为该类可以包含一个名为
entry
、类型为
AddressBookEntry
、名称相同的字段),那么参数名称将是相同的)。

我在文档中是否遗漏了这一点,或者文档中是否真的没有提到这一点?无论如何,谢谢,可以工作:-)。@yankee:基本的数据绑定功能是有文档记录的,尽管它们忘了提到
@modeldattribute
使用相同的数据绑定功能。@axtavt我的模型结构与OP的相同,但我必须使用2个
@modeldattributes
(比如一个用于AddressBookEntry,一个用于Address)。有没有办法只使用一个@modeldattribute?PS:地址嵌入在AddressBookEntry中。