最近使用springmvc完成分頁功能,遇到了一些問題,百度了一下發現都是一樣的。因此貼出自己的解決思路。
一:控制層中
@Value("#{configProperties['schoolPageSize']}")
private String schoolPageSize;
@RequestMapping(value="/schoolList")
public ModelAndView schoolList(School school,String page){
//每頁顯示的條數
int pageSize=Integer.valueOf(schoolPageSize).intValue();
List<School> schoolList=schoolService.findSchoolList(school);
ModelAndView modelAndView = new ModelAndView();
//查到的學校總數
int SchoolNum=schoolList.size();
//查到的總用戶數
modelAndView.addObject("SchoolNum",SchoolNum);
int pageTimes;
if(SchoolNum%pageSize == 0)
{
pageTimes =SchoolNum/pageSize;
}else
{
pageTimes = SchoolNum/pageSize + 1;
}
modelAndView.addObject("pageTimes", pageTimes);
//頁面初始的時候page沒有值
if(null == page)
{
page = "1";
}
//每頁開始的第幾條記錄
int startRow = (Integer.parseInt(page)-1) * pageSize;
List<School> schools= this.schoolService.getschoolByPage(startRow, pageSize);
modelAndView.addObject("currentPage", Integer.parseInt(page));
modelAndView.addObject("schools", schools);
modelAndView.setViewName("school/schoolList");
return modelAndView;
}
備注: @Value("#{configProperties['schoolPageSize']}")需要完成配置
springmvc.xml中的配置
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:value.properties</value>
</list>
</property>
</bean>
value.properties的配置
schoolPageSize=5(注意沒有“;”)
頁面的代碼
<div class="pagging">
<div class="left">共${SchoolNum}條記錄</div>
<div class="right">
<c:if test="${currentPage == 1}">
<span class="disabled"><< 前一頁</span>
</c:if>
<c:if test="${currentPage != 1}">
<a href="school/schoolList.action?page=${currentPage-1}"><< 前一頁</a>
</c:if>
<c:if test="${currentPage == 1}">
<span class="current">1</span>
</c:if>
<c:if test="${currentPage != 1}">
<a href="school/schoolList.action?page=1">1</a>
</c:if>
<%
session.getAttribute("pageTimes");
for(int i=1;i<2;i++)
{
request.setAttribute("page", i+1);
%>
<c:if test="${currentPage == page}">
<span class="current"><%=i+1%></span>
</c:if>
<c:if test="${currentPage != page}">
<a href="school/schoolList.action?page=<%=i+1%>"><%=i+1%></a>
</c:if>
<%} %>
<c:if test="${currentPage == pageTimes}">
<span class="disabled">後一頁 >></span>
</c:if>
<c:if test="${currentPage != pageTimes}">
<a href="school/schoolList.action?page=${currentPage+1}">後一頁 >></a>
</c:if>
</div>