Spring Framework - 同一类型的多个ModelAttributes [英] Spring Framework - Multiple ModelAttributes of Same Type

查看:315
本文介绍了Spring Framework - 同一类型的多个ModelAttributes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a checkout page that requires a shipping address and a billing address. This integrates with a third-party library which has these both implemented as the same type: Address. What I need to be able to do is something like:

@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应用程序,以使其工作。我可能会制作外观模型并将它们映射到控制器内部的真实模型,但如果我可以避免它,我宁愿不要沿着这条路走下去。

I am stuck on how to send two separate models of the same exact type over to my Spring application in a way that makes this work. I could potentially make facade models and map them to the real ones inside of the controller, but I would prefer not to go down that route if I can avoid it.

编辑:更改了模型属性名称,以便更清楚地解决问题区域。

Changed the model attribute names to hopefully make the problem area more clear.

推荐答案

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

Instead of creating separate model attributes for each type, create an Order object and model attribute '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

这篇关于Spring Framework - 同一类型的多个ModelAttributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆