简体   繁体   English

如何从控制器传递多个模型对象以及如何将所有作为命令对象传递给spring mvc中的form:form?

[英]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. 只需将personaddress都添加到一个名为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.

相关问题 如何将两个对象传递给同一Spring Controller Form提交? - How to pass two objects to the same Spring Controller Form submission? Spring MVC:如何将表单数据从JSP传递到控制器 - Spring mvc : How to pass the form data from jsp to controller Spring MVC:如何使用表单将模型对象传递给控制器​​方法 - Spring MVC : How to pass model object to controller method with out using form 如何通过Spring Controller中的文件传递JSON对象数组? - How to pass array of JSON objects with Files from Spring Controller? 如何从JSP中的循环将Spring MVC模型传递给控制器 - How to pass Spring MVC model to controller from a loop in JSP 如何使用Jquery将多个对象作为表单参数传递给Resful Service - How to Pass Multiple Objects to Resful Service as Form Parameter using Jquery 如何使用jQuery将包含对象数组的对象数组传递给Spring MVC控制器? - How to pass array of objects containing arrays of objects to Spring MVC controller using jQuery? Spring MVC / JSP-如何将对象的嵌套列表从选择表单传递给控制器 - Spring MVC / JSP - How to pass nested list of object to controller from a select form 如何将JSF表单数据传递给Spring Controller - How to pass jsf form data to spring controller Spring 3中如何将对象列表从视图中的表单绑定到控制器 - How to bind a list of objects from a form in the View to the Controller in Spring 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM