brn*_*nrd 17 html java spring thymeleaf
我的问题如下:
我有2个不同的对象,我要从单个表单中填充.
使用1个对象,我只需要在newFoo.html中执行:
<form th:object="${foo} th:action="@{/foo}" method="post">
<input type="text" th:field="*{name}"/>
<button type="submit">Go</button>
</form>
Run Code Online (Sandbox Code Playgroud)
并在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";
}
Run Code Online (Sandbox Code Playgroud)
假设我有另一个带有"status"变量的对象栏.如何传递该对象以便我可以在同一表单中提交输入?
喜欢:
<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>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试使用th:object中的fieldset,这不起作用,我试图将两个:对象放在表单中,这也不起作用.
我找到的唯一方法是构建一个包含这两个对象的其他对象,然后传递它.这很好,但我不能创造那种对象,这是无稽之谈(即使它有效).
当然,对象并不像Foo和Bar那么简单,否则我会合并这两个.但那不是我能做的.
甚至可以传递两个这样的对象来在表单中使用吗?
谢谢.
Sot*_*lis 24
我认为你不需要使用两个th:objects.只是用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>
Run Code Online (Sandbox Code Playgroud)
我认为Spring在控制器方面足够聪明,可以使用它的映射技术将你的字段映射到正确的命令对象foo或bar.
小智 6
我使用 div 标签包围第二个对象的表单输入,并添加了 th:object..... 控制器处理它并将其添加到数据库中。
<form method=post th:object="${object1}" >
<div th:object="${object2}" >
code......
</div>
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34577 次 |
| 最近记录: |