首页功能
自建Page类实现分页
public class Page {
private int current=1;
private int limit=10;
private int rows;
private String path;
get、set方法;
public int getTotal(){
if(rows%limit==0)
{
return rows/limit;
}else {
return rows/limit+1;
}
}
public int getFrom(){
int from=current-2;
return (from<1)?1:from;
}
public int getTo(){
int to=current+2;
int total=getTotal();
return (to>total)?total:to;
}
}
Controller:
@RequestMapping(path="/index",method = RequestMethod.GET)
public String getIndexPage(Model model, Page page, @RequestParam(name="orderMode",defaultValue = "0") int orderMode){
page.setRows(discussPostService.findDiscussPostRow(0));
page.setPath("/index?orderMode="+orderMode);
需要返回的数据处理封装
model.addAttribute("discussPosts",dis);
model.addAttribute("orderMode",orderMode);
return "/index";
}
前端
<!-- 分页 -->
<nav class="mt-5" th:if="${page.rows>0}" 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.from,page.to)}">
<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>