如何通过两种物体在使用百里香的形式中使用? [英] How to pass two objects to use in a form using thymeleaf?
问题描述
我的问题如下:
我有2个不同的对象,我必须从单个窗体中填充。
使用1个对象,我只需在newFoo.html中完成:
< form th:object =$ {foo} th:action =@ {/ foo}method =post>
< input type =textth:field =* {name}/>
< / form>
< / code>< / $ p"按钮类型=提交>前往< /按钮> $ p
和在FooController中:
pre $ @RequestMapping(value =/ foo / new,method = RequestMethod.GET)
public String newFoo(final Foo foo,Model model){
returnnewFoo;
}
@ RequestMapping(value =/ foo / new,method = RequestMethod.POST)
public String saveFoo(final Foo foo,final BindingResult bindingResult,Model model){
fooService.save(foo);
returnredirect:/ foo / new;
}
还有一个带有状态变量的其他对象栏,我该怎么做才能传递这个对象,这样我才能在同一个表单中提交输入
< form th:object = $ {foo}&& $ {bar}th:action =@ {/ foo}method =post>
< input type =textth:field =* {name}/>
< input type =textth:field =* {status}/>
< button type =submit> Go< / button>
< / form> ;
到目前为止,我试图用field中的一个th:object对象进行设置,我尝试在表单中放置两个th:object,这也是行不通的。
我发现的唯一方法是构建一个包含这些对象的对象两个对象,并通过它,这很好,但我不能创建这种类型的对象,这是无稽之谈(即使它的工作原理)。
当然,对象并不像Foo和Bar那么简单,否则我会合并这两个。但那不是我能做到的。
是否有可能通过两个这样的对象在表单中使用?
已经感谢您。
<我不认为你需要使用两个 th:objects
。只需使用 th:value
< form th:action = @ {/ foo}method =post>
< input type =textth:value =$ {foo.name}name =name/>
< input type =textth:value =$ {bar.status}name =status/>
< button type =submit> Go< / button>
< / form>
我认为Spring在控制器方面足够智能,可以使用它的映射技术来映射字段到他们正确的命令对象,foo或bar。
My problem is the following :
I've 2 differents objects that I've to fill from a single form.
With 1 object, I simply do in the newFoo.html :
<form th:object="${foo} th:action="@{/foo}" method="post">
<input type="text" th:field="*{name}" />
<button type="submit">Go</button>
</form>
and in the FooController :
@RequestMapping(value = "/foo/new", method = RequestMethod.GET)
public String newFoo(final Foo foo, Model model) {
return "newFoo";
}
@RequestMapping(value = "/foo/new", method = RequestMethod.POST)
public String saveFoo(final Foo foo, final BindingResult bindingResult, Model model) {
fooService.save(foo);
return "redirect:/foo/new";
}
Let's say I've an other object bar with a "status" variable in it. How can I do to pass that object so I can submit the input within the same form ?
Like :
<form th:object="${foo} && ${bar}" th:action="@{/foo}" method="post">
<input type="text" th:field="*{name}" />
<input type="text" th:field="*{status}" />
<button type="submit">Go</button>
</form>
So far I tried to do with to fieldset with a th:object in it, that doesn't work, I tried to put two th:object in the form, that doesn't work either.
The only way I found is to build an other object containing those two objects, and pass it. That works well, but I can't create that kind of object, it's nonsense (even if it works).
Of course, the objects aren't as simple as Foo and Bar here, otherwise I would have merge those two. But that's not something I can do.
Is it even possible to pass two objects like that to use in a form ?
Thanks already.
I don't think you need to use two th:objects
. Just use th:value
<form th:action="@{/foo}" method="post">
<input type="text" th:value="${foo.name}" name="name"/>
<input type="text" th:value="${bar.status}" name="status"/>
<button type="submit">Go</button>
</form>
I would think Spring is smart enough, on the controller side, to use its mapping techniques to map your fields to their proper command object, foo or bar.
这篇关于如何通过两种物体在使用百里香的形式中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!