[英]How to pass multiple model objects from controller and how to pass all as command objects into the form:form in spring mvc?
The scenario is like I want to pass more than one model objects from controller which I can achieve like, 场景就像我想从控制器传递多个模型对象一样,我可以实现,
model .addAttribute(“person”, new Person());
model.addAttribute(“address”, new Address());
But how to pass both of them or more in spring view 但是如何在春季视图中同时通过两者或更多
<form:form action=”registration” command=”person”>
As it only allows me to pass only one command . 因为它只允许我传递一个命令。 Then how to pass all the require command over their.
然后如何将所有require命令传递给它们。
Just add both person
and address
into an enveloping object called personDetails
and pass that to the form. 只需将
person
和address
都添加到一个名为personDetails
对象中,然后将其传递给表单即可。
class PersonDetails {
private Person person;
private Address address;
...
}
PersonDetails personDetails = new PersonDetails();
personDetails.setPerson(new Person());
personDetails.setAddress(new Address());
model.addAttribute(“personDetails”, personDetails);
<form:form action=”registration” command=”personDetails”>
Once you do that, you'll have to modify the path
s appropriately. 完成此操作后,您将必须适当地修改
path
。
<form:input path="firstName" />
becomes 变
<form:input path="person.firstName" />
Other more elegant option is to enclose Address
within the Person
and just expose the Person
as a command. 其他更优雅的选择是将
Address
封装在Person
内,然后将Person
作为命令公开。 Just as @Seabook mentioned in his answer below. 就像@Seabook在下面的答案中提到的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.