springMVC+thymeleafform表单提交前后台数据传递

来源:互联网 发布:sql connect by prior 编辑:程序博客网 时间:2024/06/05 10:28

一个spring mvc + thymeleafform 简单例子

后端

@RequestMapping(value = "/add", method=RequestMethod.POST)  public String save(@ModelAttribute(value="message") Message message) {    ...  }

前端:

<form action="#" th:action="@{/add}" th:object="${message}" method="post">    <input type="text" th:field="*{info}" />    <input type="submit" />  </form>


特别注意 th:field
这个属性在Spring-mvc里很重要,承担着绑定后台Bean属性的重任
th:field必须是选择表达式(*{…})。



Message实体类:

public class Message {    private String info;     public String getInfo() {      return info;    }     public void setInfo(String info) {      this.info= info;    }  }



原创粉丝点击