Spring MVC多表单后备对象

时间:2017-11-12 14:50:20

标签: spring spring-mvc

在请求中说,我正在

?name=Jack&age=26&price=100&quantity=2

我有两个模型对象,如下所示

public class User {

public String name;
public String age;

//getters and setters

}

public class Order {

public int price;
public int quantity;

//getters and setters

}

现在我想要一个控制器方法,如下所示

@RequestMapping(value = "/submit", method = RequestMethod.GET)
public String home(@ModelAttribute("user") User user, @ModelAttribute("order") Order order, Model model) {
    //stuff
}

如您所见,我想使用两个单独的@ModelAttribute批注填充模型对象。这在春天mvc有可能吗?

1 个答案:

答案 0 :(得分:0)

Sattyaki,我建议你为User和Order提供一些getter / setter,然后用另一个类组成所需的请求:

'union'

要请求此操作,只需使用您的参数向所需的URI(/ submit)发出GET。观察他们现在使用点符号:

public class Checkout {

    private User user;
    private Order order;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}