底层Dao
import java.util.List; import org.cyxl.ssh.page.IPage; public class PageImpl implements IPage{ /** * * @param list2 所要进行分页的集合 * @param totalCount 总条数 * @param pageSize 一页多少记录 * @param pageCount 分页数 * @param page 当前页面 * @return */ public List page(List list2,Integer totalCount,Integer pageSize,Integer pageCount,Integer page){ List list; if(page==1){ //首页,前一页不显示 if(totalCount<pageSize){ list = list2.subList(0,totalCount); }else{ list = list2.subList(0,pageSize); } }else if(page==pageCount){ //尾页,后一页不显示 list = list2.subList(page*pageSize-pageSize,totalCount); }else{ //通通显示 list = list2.subList((page-1)*pageSize,page*pageSize); } return list; } }
需要注意的地方,先看下jsp:
<center> <div style="font-size: 20px"> <s:if test = "%{totalCount<=pageSize}"> 第1页,共1页,共有<s:property value = "totalCount"/>条记录 </s:if> <s:else> <s:if test = "%{page==1}"> <s:iterator value = "array" id = "num"> <s:if test="%{#num==page}"> <span style="font-size: 30px; color: black">${num}</span> </s:if> <s:else> <a href=<s:url value = "index.action" > <s:param name = "page">${num}</s:param> </s:url>> ${num} </a> </s:else> </s:iterator> <a href=<s:url value = "index.action" > <s:param name = "page"><s:property value = "page+1"/></s:param> </s:url>> Next </a> <a href=<s:url value = "index.action" > <s:param name = "page"><s:property value = "pageCount"/></s:param> </s:url>> Last </a> 第1页,共<s:property value="pageCount" />页,共有<s:property value = "totalCount"/>条记录 </s:if> <s:elseif test="%{page==pageCount}"> <a href=<s:url value = "index.action" > <s:param name = "page" value = "1"/> </s:url>> Home</a> <a href=<s:url value = "index.action" > <s:param name = "page"><s:property value = "page-1"/></s:param> </s:url>> Prev </a> <s:iterator value = "array" id = "num"> <s:if test="%{#num==page}"> <span style="font-size: 30px; color: black">${num}</span> </s:if> <s:else> <a href=<s:url value = "index.action" > <s:param name = "page">${num}</s:param> </s:url>> ${num} </a> </s:else> </s:iterator> 第<s:property value="pageCount" />页,共<s:property value="pageCount" />页,共有<s:property value = "totalCount"/>条记录 </s:elseif> <s:else> <a href=<s:url value = "index.action" > <s:param name = "page" value = "1"/> </s:url>> Home </a> <a href=<s:url value = "index.action" > <s:param name = "page"><s:property value = "page-1"/></s:param> </s:url>> Prev </a> <s:iterator value = "array" id = "num"> <s:if test="%{#num==page}"> <span style="font-size: 30px; color: black">${num}</span> </s:if> <s:else> <a href=<s:url value = "index.action" > <s:param name = "page">${num}</s:param> </s:url>> ${num} </a> </s:else> </s:iterator> <a href=<s:url value = "index.action" > <s:param name = "page"><s:property value = "page+1"/></s:param> </s:url>> Next </a> <a href=<s:url value = "index.action" > <s:param name = "page"><s:property value = "pageCount"/></s:param> </s:url>> Last </a> 第<s:property value="page" />页,共<s:property value="pageCount" />页,共有<s:property value = "totalCount"/>条记录 </s:else> </s:else> </div> </center>
需要注意的地方:小心总条数小于一页规定的条数大小,发生这种情况不要出现首页上一页......
这里面需要传2个参数进去,page和pageCount,所以在Action中要Get、Set。我的Action:
public String selete(){ String field_name = "typ"; String field_value = "1"; String desc = "desc"; String table = "Content"; List list2 = userService.selete(field_name,field_value,desc,table); totalCount = list2.size(); if(totalCount%pageSize==0){ pageCount = totalCount/pageSize; }else{ pageCount = totalCount/pageSize+1; } array = new ArrayList(); for(int i=1;i<=pageCount;i++){ array.add(i); } //给了list,总记录的数目,还有一页显示多少条记录,总页数,当前所在页,要求生成------ list = pageService.page(list2, totalCount, pageSize, pageCount, page); return "selete"; }
参数一定要给正确!!!