[英]How to pass multiple model objects from controller and how to pass all as command objects into the form:form in spring mvc?
[英]How to pass two objects to the same Spring Controller Form submission?
我有以下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
}
和以下控制器:
@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
}
}
以及以下表單摘要:
<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>
提交表單時出現以下錯誤:
java.lang.IllegalArgumentException: argument type mismatch
如果對象不同並且pojo具有不同的屬性,則可以正常工作。 有沒有辦法使這項工作?
我只是想通了。 訣竅是將pojos嵌套到另一個pojo中。
public class Nest {
@Valid
private Foo one = null;
@Valid
private Foo two = null;
.... getters and setters
}
使用這樣的控制器:
@Controller
@RequestMapping(value = "/path", method = RequestMethod.POST)
public class Control {
public String handler(@Valid Nest nest, BindingResult res_nest){
//Business logic
}
}
和這樣的形式:
<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>
這確實使分別驗證兩個對象成為不可能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.