springboot 整合 thymeleaf 及常用标签的使用方法,美的 java 面试流程
@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {
@RequestMapping("finduser")
public String finduser(ModelMap map) {
User u = new User();
u.setName("heng");
u.setAge(21);
u.setUrl("https://blog.csdn.net/qq_39313596");
u.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
map.addAttribute("u",u);
java.util.List<User> list = new ArrayList<User>();
User u2 = new User();
u2.setName("heng");
u2.setAge(17);
u2.setUrl("https://blog.csdn.net/qq_39313596");
u2.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
User u3 = new User();
u3.setName("heng");
u3.setAge(19);
u3.setUrl("https://blog.csdn.net/qq_39313596");
u3.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
User u4 = new User();
u4.setName("heng");
u4.setAge(21);
u4.setUrl("https://blog.csdn.net/qq_39313596");
u4.setDesc("<a href='https://blog.csdn.net/qq_39313596'>博客地址</a>");
list.add(u2);
list.add(u3);
list.add(u4);
map.addAttribute("list", list);
return "/thymeleaf/index";
}
@PostMapping("saveUser")
public String saveUser(User u) {
System.out.println(u);
return "redirect:../thymeleaf/finduser";
}
}
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p th:text="${u.name}"></p>
<p th:text="${u.age}"></p>
<p th:text="${u.url}"></p>
<p th:text="${u.desc}"></p>
<p th:utext="${u.desc}"></p>
<div th:object="${u}">
<p th:text="*{name}"></p>
<p th:text="*{age}"></p>
<p th:text="*{ur
l}"></p>
<p th:text="*{desc}"></p>
<p th:utext="*{desc}"></p>
</div>
<form th:action="@{saveUser}" method="post" th:object="${u}" th:method="post">
<input type="text" th:field="*{name}"/>
<input type="text" th:field="*{age}"/>
<input type="text" th:field="*{url}"/>
<input type="text" th:field="*{desc}"/>
<input type="submit"/>
</form>
<select>
<option >选择框</option>
<option th:selected="${u.name eq 'heng'}">heng</option>
<option th:selected="${u.name eq 'LeeCX'}">LeeCX</option>
</select>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>年龄备注</th>
</tr>
<tr th:each="person:${list}">
<td th:text="${person.name}"></td>
<td th:text="${person.age}"></td>
<td th:text="${person.age gt 18} ? 你成熟了 : 你还年轻">18 岁</td>
</tr>
</table>
<br/>
<br/>
<div th:switch="${u.name}">
<p th:case="'lee'">lee</p>
<p th:case="A">普通管理员</p>
<p th:case="heng">超级管理员</p>
<p th:case="*">其他用户</p></div><br/></body>
评论