最近写SSM的项目,需要用到分页,但是使用PageHelper插件的分页老是出现问题,于是使用了老师发的demo里面的分页方法。
Page.java
package com.zhc.util;
public class Page {
int start=0;
int count = 5;
int last = 0;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getLast() {
return last;
}
public void setLast(int last) {
this.last = last;
}
public void caculateLast(int total) {
// 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
if (0 == total % count)
last = total - count;
// 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
else
last = total - total % count;
}
}
applicationContext.xml
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!-- 使用下面的方式配置参数,一行配置一个 -->
<value>
</value>
</property>
</bean>
</array>
</property>
</bean>
Controller.java
@RequestMapping("/list")
public ModelAndView fileList(Page page, HttpServletRequest request,HttpServletResponse response){
ModelAndView mv = new ModelAndView();
//分页
PageHelper.offsetPage(page.getStart(),5);
List<FilePojo> list = fileService.selectAll();//调用Service中查找语句
int total = (int) new PageInfo<>(list).getTotal();
page.caculateLast(total);
// 放入转发参数
mv.addObject("list", list);
// 放入jsp路径
mv.setViewName("blog/file");
return mv;
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<p>共 <fmt:formatNumber type="number" value="${page.last/page.count+1 }" maxFractionDigits="0"/> 页
第 <fmt:formatNumber type="number" value="${page.start/page.count+1 }" maxFractionDigits="0"/>页</p>
<c:if test="${page.start!=0 }">
<a href="admin/file/list?start=0">首 页</a>
<a href="admin/file/list?start=${page.start-page.count}">上一页</a>
</c:if>
<c:if test="${page.start!=page.last }">
<a href="admin/file/list?start=${page.start+page.count}">下一页</a>
<a href="admin/file/list?start=${page.last}">末 页</a>
</c:if>
但是不太明白为什么Controller中并没有向jsp中传递page这个参数,但是jsp中却可以调用