[英]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.