将数据从html发送到Thymeleaf中的控制器? [英] Send datas from html to controller in Thymeleaf?

查看:317
本文介绍了将数据从html发送到Thymeleaf中的控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您可以在如本教程所建议,您需要使用th:objectth:actionth:field在Thymeleaf中创建表单.

As the tutorial suggests, you need to use th:object, th:action and th:field to create a form in 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;
  }
}

希望这会有所帮助.

这篇关于将数据从html发送到Thymeleaf中的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆