--thymeleaf提交form表单
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link th:src="@{/css/main.css}" rel="stylesheet" type="text/css"/>
<script th:src="@{/js/ydaq.js}" ></script>
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
<p>Id: <input type="text" th:field="*{id}" /></p>
<p>Message: <input type="text" th:field="*{content}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting) {
return "result";
}
<!--分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!--通用Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.9</version>
</dependency>
2、控制器实现pageInfo构造,需要返回pageInfo对象。对象默认配置可以更改
@GetMapping("/showAllDb")
public String showParams(Model model,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize) {
PageInfo pageInfo = mserverDbserverService.findAll(pageNum, pageSize);
//获得当前页
model.addAttribute("pageNum", pageInfo.getPageNum());
//获得一页显示的条数
model.addAttribute("pageSize", pageInfo.getPageSize());
//是否是第一页
model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
//获得总页数
model.addAttribute("totalPages", pageInfo.getPages());
//是否是最后一页
model.addAttribute("isLastPage", pageInfo.isIsLastPage());
model.addAttribute("mserverDbserverList", pageInfo.getList());
return "dbshow";
}
3、html支持分页功能,
th:href="@{${'/showAllDb'}为数据源的请求url,这样即实现了显示共多少页数据,以及点击展示确定页数的数据功能。
<div >
<nav>
<ul >
<li><a href="">«</a></li>
<li><a th:if="${not isFirstPage}"
th:href="@{${'/showAllDb'}(pageNum=${pageNum-1},pageSize=${pageSize},type=2)}">Previous</a>
<a th:if="${isFirstPagee}" href="javascript:void(0);">Previous</a>
</li>
<li th:each="pageNo:${#numbers.sequence(1, totalPages)}">
<a th:if="${pageNum eq pageNo}" href="javascript:void(0);">
<span th:text="${pageNo}"></span>
</a> <a th:if="${not (pageNum eq pageNo)}"
th:href="@{${'/showAllDb'}(pageNum=${pageNo},size=${pageSize},type=2)}">
<span th:text="${pageNo}"></span>
</a>
</li>
<li><a th:if="${not isLastPage}"
th:href="@{${'/showAllDb'}(pageNum=${pageNum+1},size=${pageSize},type=2)}">Next</a>
<a th:if="${isLastPage}" href="javascript:void(0);">Next</a>
</li>
<li><a href="#">»</a></li>
</ul>
</nav>
</div>
相关阅读:thymeleaf - java web页面模板引擎介绍(上篇)
本文来自网易实践者社区,经作者王海鹏授权发布。