Spring Framework – 同一类型的多个ModelAttributes

我正在结帐页面,需要送货地址和帐单邮寄地址。 它与第三方库集成,这些库都实现了相同的类型:地址。 我需要做的是:

@RequestMapping(method = RequestMethod.POST) @ResponseBody public Response createOrder( @ModelAttribute("customer") Customer customer, @ModelAttribute("shipping") Address shippingAddress, @ModelAttribute("payment") Payment paymentInformation, @ModelAttribute("billing") Address billingAddress ) { // Create the order } 

我坚持如何将两个相同类型的独立模型发送到我的Spring应用程序,以使其工作。 我可以制作外观模型并将它们映射到控制器内部的真实模型,但如果我可以避免它,我宁愿不去那条路线。

编辑:更改模型属性名称以希望使问题区域更清晰。

不是为每种类型创建单独的模型属性,而是创建Order对象和模型属性“order”。

  //Order class private Customer customer; private Address shippingAddress; private Payment paymentInformation; private Address billingAddress .. public Response createOrder( @ModelAttribute("order") Order order) { // Create the order } 

然后请求看起来像

 shippingAddress.city=foo&billingAddress.city=bar