spring 将数据从 html 发送到 Thymeleaf 中的控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17669212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send datas from html to controller in Thymeleaf?
提问by user978758
I must send datas from html page (simple form with few input text fields) to page controller and then to database. I am using thymeleaf 2.0.17, spring 3.0. I searched and checked some solutions but didn't work. Maybe someone had the same problem and find some good solution. Please help. Thanks
我必须将数据从 html 页面(具有很少输入文本字段的简单表单)发送到页面控制器,然后发送到数据库。我正在使用 thymeleaf 2.0.17, spring 3.0。我搜索并检查了一些解决方案,但没有奏效。也许有人遇到了同样的问题并找到了一些好的解决方案。请帮忙。谢谢
回答by Shinichi Kai
You can find an example in http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#creating-a-form.
您可以在http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#creating-a-form 中找到示例 。
As the tutorial suggests, you need to use th:object, th:actionand th:fieldto create a form in Thymeleaf.
正如教程所建议的,您需要使用th:object,th:action并th:field在 Thymeleaf 中创建一个表单。
It looks like this:
它看起来像这样:
Controller:
控制器:
@RequestMapping(value = "/showForm", method=RequestMethod.GET)
public String showForm(Model model) {
Foo foo = new Foo();
foo.setBar("bar");
model.addAttribute("foo", foo);
...
}
@RequestMapping(value = "/processForm", method=RequestMethod.POST)
public String processForm(@ModelAttribute(value="foo") Foo foo) {
...
}
html:
html:
<form action="#" th:action="@{/processForm}" th:object="${foo}" method="post">
<input type="text" th:field="*{bar}" />
<input type="submit" />
</form>
Foo.java:
Foo.java:
public class Foo {
private String bar;
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
Hope this helps.
希望这可以帮助。

