【问题标题】:Send datas from html to controller in Thymeleaf?将数据从 html 发送到 Thymeleaf 中的控制器?
【发布时间】:2013-07-14 04:49:49
【问题描述】:

我必须将数据从 html 页面(输入文本字段很少的简单表单)发送到页面控制器,然后再发送到数据库。我正在使用 thymeleaf 2.0.17,spring 3.0。我搜索并检查了一些解决方案,但没有奏效。也许有人遇到了同样的问题并找到了一些好的解决方案。请帮忙。谢谢

【问题讨论】:

  • 如果您正在搜索例如,那么您可以在这里找到它thymeleaf.org/documentation.html,如果您遇到问题,请重新表述您的问题,说明真正的问题是什么(即在绑定中,在持久性中..)

标签: spring spring-mvc thymeleaf


【解决方案1】:

你可以在http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#creating-a-form找到一个例子。

按照教程的建议,您需要使用 th:objectth:actionth:field 在 Thymeleaf 中创建表单。

看起来像这样:

控制器:

@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:

<form action="#" th:action="@{/processForm}" th:object="${foo}" method="post">
  <input type="text" th:field="*{bar}" />
  <input type="submit" />
</form>

Foo.java:

public class Foo {
  private String bar;

  public String getBar() {
    return bar;
  }

  public void setBar(String bar) {
    this.bar = bar;
  }
}

希望这会有所帮助。

【讨论】:

  • 以上链接无法访问。
  • 我已经修复了断开的链接。
  • 对我不起作用。我收到以下错误:Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "fragments/send" - line 21, col 55) 当我在表单上使用 th:field 标记时。
猜你喜欢
  • 1970-01-01
  • 2015-07-02
  • 2020-04-28
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
相关资源
最近更新 更多