具体业务场景参照:【SpringBoot仿牛客网】02、开发社区首页
0 需求分析
要实现分页,传给页面的数据要有:
指定行数的内容(即:当前页面内容)
分页区页码范围(如图中的1-5页)
当前页码
每页显示多少条数据
一共有多少条数据
需要的数据很多很繁琐,我们直接把这些数据封装成一个实体类Page,在浏览器请求时通过model将page对象传入Controller方法中,赋予page对象属性,再通过model将page对象传入动态模板中,供页面使用
1 Page类
/**
* 封装分页相关的信息
*/
public class Page {
private int current=1;//当前页码
private int limit=10;//每页显示多少数据
private int rows;//总共有多少条数据
private int showPages=5;//下面的页码一次显示5页
private String path;
public int getShowPages() {
return showPages;
}
public void setShowPages(int showPages) {
if(showPages<=20 && showPages>=1) {
this.showPages = showPages;
}
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
if(rows>=0) {
this.rows = rows;
}
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
if(current>=1) {
this.current = current;
}
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
if(limit>=1 && limit<=100) {
this.limit = limit;
}
}
/**
* 获取总共有多少页
*/
public int getTotal(){
if(rows%limit==0)
return rows/limit;
else
return rows/limit+1;
}
//页面上要显示从几页到几页
/**
* 获取起始显示页
*/
public int getStartPage(){
int start = current-2;
//如果current是最后两页,前面就多显示几页
int total=getTotal();
if(total-start<=showPages-1)
start=total-showPages+1;
return start<1?1:start;
}
/**
* 获取终止显示页
*/
public int getEndPage(){
int end = current+2;
//如果current是最前面两页,后面就多显示几页
end=end<showPages?showPages:end;
int total=getTotal();
return end>total?total:end;
}
/**
* 起始查询行
*/
public int getStartRow(){
return (current-1)*limit;
}
}
2 Controller方法
//访问首页
@RequestMapping(path = "/index",method = RequestMethod.GET)
public String getIndex(Model model, Page page){
//这俩属性没有默认值,需要自己设定
page.setRows(discussService.findRowsCount(0));
page.setPath("/index");//用于前端的链接,格式为/index?current=1
List<DiscussPost> list = discussService.findDiscussPost(0,page.getStartRow(),page.getLimit());
List<Map<String,Object>> discussPosts = new ArrayList<>();
if(!list.isEmpty()){
for(DiscussPost post:list){
Map<String, Object> map = new HashMap<>();
//帖子
map.put("post",post);
User user = userService.findUserById(post.getUserId());
map.put("user", user);
discussPosts.add(map);
}
}
model.addAttribute("discussPosts",discussPosts);
return "/index";
}
3 动态模板
index.html分页部分
<!-- 分页 -->
<nav class="mt-5" th:if="${page.rows>page.limit}" th:fragment="pagination">
<ul class="pagination justify-content-center">
<li class="page-item">
<a class="page-link" th:href="@{|${page.path}?current=1|}">首页</a>
</li>
<li th:class="|page-item ${page.current==1?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=${page.current-1})}">上一页</a></li>
<li th:class="|page-item ${i==page.current?'active':''}|" th:each="i:${#numbers.sequence(page.startPage,page.endPage)}">
<a class="page-link" th:href="@{${page.path}(current=${i})}" th:text="${i}">1</a>
</li>
<li th:class="|page-item ${page.current==page.total?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=${page.current+1})}">下一页</a>
</li>
<li class="page-item">
<a class="page-link" th:href="@{${page.path}(current=${page.total})}">末页</a>
</li>
</ul>
</nav>
4 复用分页模块
以后想复用分页模块很简单
1.在对应的Controller方法里传入Page page,设置page.setRows()和page.setPath()
2.在动态页面复用index.html的分页部分