###2、Bean类
package cn.hadron.springboot.bean;
import java.io.Serializable;
public class UserBean implements Serializable{
private Integer id;
private String username;
private String password;
private String birthday;
public UserBean(){}
public UserBean(String username, String password, String birthday) {
this.username = username;
this.password = password;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
package cn.hadron.springboot.controller;
import cn.hadron.springboot.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
public class IndexController {
@RequestMapping("/")
public String index(Model model){
System.out.println("LoginController.index");
//根据Thymeleaf默认模版,映射到resources/templates/index.html
return "index";
}
@RequestMapping("/test")
public String test(HttpServletRequest request, HttpSession session){
System.out.println("IndexController.test");
request.setAttribute("msg","Hello,World!");
session.setAttribute("tip","Hello,Java!");
request.getServletContext().setAttribute("name","Hello,SpringBoot 2.x!");
return "test";
}
@RequestMapping("/testIf")
public String testIf(WebRequest web){
System.out.println("IndexController.testIf");
web.setAttribute("username","Hadron Cheng",WebRequest.SCOPE_REQUEST);
web.setAttribute("password","123456",WebRequest.SCOPE_REQUEST);
return "testIf";
}
@RequestMapping("/testEach")
public String testEach(WebRequest web){
System.out.println("IndexController.testEach");
List<UserBean> list=new ArrayList<>();
list.add(new UserBean("abc","123","1990-09-10"));
list.add(new UserBean("test","123","1991-09-10"));
list.add(new UserBean("hadron","123","1992-09-10"));
web.setAttribute("users",list,WebRequest.SCOPE_REQUEST);
return "list";
}
}
(1)index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="main">
<h4>Thymeleaf</h4>
<a th:href="@{test?username=hadron}">测试数据访问</a><br>
<a th:href="@{testIf}">if条件判断</a><br>
<a th:href="@{testEach}">循环测试</a><br>
</div>
</body>
</html>
提示:
(2)test.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="main">
param:<span th:text="${param.username}">动态模版</span><br>
request:<span th:text="${msg}">动态模版</span><br>
session:<span th:text="${session.tip}">动态模版</span><br>
application:<span th:text="${application.name}">动态模版</span>
</div>
</body>
</html>
(3)testIf.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="main">
<span th:if="${username!=null}">username字段非空</span><br>
<span th:if="${password==null}">age字段空</span><br>
<span th:unless="${role!=null}">与th:if相反</span>
</div>
</body>
</html>
(4)list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="main">
<table border="1">
<tr>
<th>序号</th>
<th>账号</th>
<th>口令</th>
<th>出生日期</th>
</tr>
<tr th:each="user,stat:${users}">
<td th:text="${stat.index}">状态变量</td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.birthday}"></td>
</tr>
</table>
</div>
</body>
</html>