package wx.demo.controllers;
/*
- 作者:planetwalker
*Conctroller类 - */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import wx.demo.entity.UserInfo;
import wx.demo.service.UserInfoService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Controller
public class WebController {
@Resource
private UserInfoService userInfoService;
@RequestMapping("/")
public ModelAndView Pages(@RequestParam(required=true,defaultValue="1")Integer page, HttpServletRequest request){
//pageNum为页面数pageSize为数据条数
PageHelper.startPage (page,3);
List<UserInfo> list = userInfoService.selectByList ();
request.setAttribute ("page",page);
ModelAndView modelAndView=new ModelAndView("index");
PageInfo<UserInfo> p=new PageInfo<UserInfo>(list);
modelAndView.addObject ("userInfolist",list);
modelAndView.addObject ("page",p);
return modelAndView;
}
}
实体层
package wx.demo.entity;
/*
-
作者:planetwalker
-
*/
public class UserInfo {
private Integer id;
private String email;
private String username;
private String password;
private String dateTime;public Integer getId() { return id; } public void setId(int id) { this.id = id; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } 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 getDateTime() { return dateTime; } public void setDateTime(String dateTime) { this.dateTime = dateTime; } @Override public String toString() { return "UserInfo{" + "id=" + id + ", email='" + email + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + ", dateTime=" + dateTime + '}'; }
}
分页插件的pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
分页页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>index</title>
<script type="text/javascript" th:src="@{/jquery/jquery-3.2.1.js}"></script>
</head>
<body>
<a href="/login">登录</a>|<a href="/regist">注册</a>
<table border="1" align="center">
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>注册时间</th>
<th>修改</th>
<th>删除</th>
</tr>
<tr th:each="userInfo ,state: ${userInfolist}">
<td th:text="${userInfo.id}"></td>
<td th:text="${userInfo.username}"></td>
<td th:text="${userInfo.password}"></td>
<td th:text="${userInfo.dateTime.replace('.0','')}"></td>
<td><a th:href="@{/toEdit(id=${userInfo.id})}">edit</a></td>
<td><a th:href="@{/delete(id=${userInfo.id})}">delete</a></td>
</tr>
</table>
<div align="center" style="margin-top:2%;">
总共<a th:text="${page.pages}"/>页|
第<a th:text="${page.getPageNum()}"/>页|
<a th:href="@{/(page=${page.firstPage})}" class="f">首页</a>|
<a th:href="@{/(page=${page.prePage})}" class="f">上一页</a>|
<a th:href="@{/(page=${page.nextPage})}" class="la">下一页</a>|
<a th:href="@{/(page=${page.lastPage})}" class="la">尾页</a>
<input type="hidden" id="first" th:value="${page.getPageNum()}"/>
<input type="hidden" id="last" th:value="${page.pages}"/>
</div>
</body>
<script type="text/javascript">
$(function () {
pageNum = $("#first").val();
lastPage = $("#last").val();
if (lastPage == pageNum) {
$('.la').removeAttr('href');//去掉a标签中的href属性
$('.la').removeAttr('onclick');//去掉a标签中的onclick事件
} else {
if (pageNum == 1) {
$('.f').removeAttr('href');//去掉a标签中的href属性
$('.f').removeAttr('onclick');//去掉a标签中的onclick事件
} else if (pageNum != 1) {
$('.f').attr('', 'href');//添加a标签中的href属性
$('.f').attr('', 'onclick');//添加a标签中的onclick事件)
}
$('.la').attr('', 'href');//添加a标签中的href属性
$('.la').attr('', 'onclick');//添加a标签中的onclick事件)
}
/*if(lastPage==pageNum){
$('.l').removeAttr('href');//去掉a标签中的href属性
$('.l').removeAttr('onclick');//去掉a标签中的onclick事件
}else{
$('.l').attr('','href');//添加a标签中的href属性
$('.l').attr('','onclick');//添加a标签中的onclick事件)
}*/
});
</script>
</html>