简体   繁体   English

如何将两个对象传递给同一Spring Controller Form提交?

[英]How to pass two objects to the same Spring Controller Form submission?

I have the following pojo: 我有以下pojo:

public class Foo {
    @Size(min=0,max=10)
    private String  bar = null;

    @Size(min=0,max=10)
    private String  baz = null;

    .... getters and setters
    }

and the following Controller: 和以下控制器:

@Controller
@RequestMapping(value = "/path", method = RequestMethod.POST)
public class Control {
    public String handler(@Valid Foo foo1, BindingResult res_foo1, @Valid Foo foo2, BindingResult res_foo2){
             //Business logic
        }
    }

and the following form snippet: 以及以下表单摘要:

<form action="/path">
    <input name="foo1.bar" type="text" />
    <input name="foo1.baz" type="text" />
    <input name="foo2.bar" type="text" />
    <input name="foo2.baz" type="text" />
</form>

I get the following error when submitting the form: 提交表单时出现以下错误:

java.lang.IllegalArgumentException: argument type mismatch

If the objects are different and the pojos have different properties it works fine. 如果对象不同并且pojo具有不同的属性,则可以正常工作。 Is there a way to make this work? 有没有办法使这项工作?

I just figured it out. 我只是想通了。 The trick is to nest the pojos into another pojo. 诀窍是将pojos嵌套到另一个pojo中。

public class Nest {
    @Valid
    private Foo one = null;

    @Valid
    private Foo two = null;
    .... getters and setters
}

use a controller like this: 使用这样的控制器:

@Controller
@RequestMapping(value = "/path", method = RequestMethod.POST)
public class Control {
    public String handler(@Valid Nest nest, BindingResult res_nest){
             //Business logic
    }
}

and a form like this: 和这样的形式:

<form action="/path">
    <input name="one.bar" type="text" />
    <input name="one.baz" type="text" />
    <input name="two.bar" type="text" />
    <input name="two.baz" type="text" />
</form>

This does make validating the two objects separately, impossible. 这确实使分别验证两个对象成为不可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从控制器传递多个模型对象以及如何将所有作为命令对象传递给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? AngularJS Spring控制器表单提交 - Angularjs Spring controller form submission 如何将JSF表单数据传递给Spring Controller - How to pass jsf form data to spring controller Spring MVC表单提交,填充在同一页面上 - Spring MVC form submission, populate on the same page 在同一页面处理表单提交 Spring Boot - Handle form submission on same page Spring Boot 如何通过Spring Controller中的文件传递JSON对象数组? - How to pass array of JSON objects with Files from Spring Controller? 如何通过名单 <String> 从控制器到 <form:select> 在春季4.0.0 - How to pass List<String> from controller to <form:select> in Spring 4.0.0 Spring 3中如何将对象列表从视图中的表单绑定到控制器 - How to bind a list of objects from a form in the View to the Controller in Spring 3 使用Spring MVC提交JakartaVelocity 1.7表单,在控制器中获取空值 - JakartaVelocity 1.7 form submission with Spring MVC, getting null values in controller Spring MVC:如何将表单数据从JSP传递到控制器 - Spring mvc : How to pass the form data from jsp to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM