@RequestMapping("/index")
public String index(ModelMap map){
//單個數據
map.put("username", "入門案例");
UserForm user = new UserForm();
user.setPassword("test_ps");
user.setUsername("test");
map.put("userInfo", user);
return "admin/index";
}
通過${}和*{}的方式來讀取表單類對象
<!DOCTYPE html>
<!-- 需要添加
<html xmlns:th="http://www.thymeleaf.org">
這樣在後面的th標籤就不會報錯
-->
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title th:text="${username}">xx</title>
</head>
<body>
<h1 th:text="${username}">Hello World</h1>
<h1>獲取對象信息</h1>
<h2>1、通過直接訪問對象的方式</h2>
<p th:text="${userInfo.username}"></p>
<p th:text="${userInfo.password}"></p>
<h2>2、通過th:object訪問對象的方式</h2>
<div th:object="${userInfo}">
<p th:text="*{username}"></p>
<p th:text="*{password}"></p>
</div>
<h1>表單提交</h1>
<!-- 表單提交用戶信息,注意字段的設置,直接是*{} -->
<form action="#" th:action="@{/add}" th:object="${userInfo}" method="post">
<input type="text" th:field="*{username}" />
<input type="text" th:field="*{password}" />
<input type="submit" />
</form>
</body>
</html>
@ResponseBody
@RequestMapping(value="/add",method=RequestMethod.POST)
public String add(@ModelAttribute UserForm user){
String username = user.getUsername();
String password = user.getPassword();
return username+"__"+password;
}