先定义一个公共类用来存放4大属性+返回的泛型结果
public class PageSupport<T>{ //页面大小 private int pageSize=1; //总记录数 private int totalCount; //当前页面 private int currentPage=1; //页面总数 private int totalPage; //查询结果 private List<T> result; //返回查询结果 public List<T> getResult() { return result; } public void setResult(List<T> result){ this.result=result; } //返回页面大小 public int getPageSize() { return pageSize; } //设置页面大小 public void setPageSize(int pageSize){ this.pageSize=pageSize; } //返回总记录数 public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } //返回当前页 public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage=currentPage; } //返回总页面数 public void setTotalPage(int totalPage) { this.totalPage=totalPage; } public int getTotalPage(){ return totalPage; } }
然后在页面的最底下插入分页的jsp
<!-- 分页功能 --> <div id="fenye"> <input type="hidden" id="currentPage" name="currentPage" value="${systemconfigPageSupport.currentPage}"/> <input type="hidden" id="totalPage" name="totalPage" value="${systemconfigPageSupport.totalPage}"/> <ul> </ul> </div>
下面是分页功能的样式表
/*分页*/ #fenye{ width: 960px; margin-left:auto; margin-right: auto; } #fenye ul{ margin-top:20px; line-height: 28px; height: 28px; text-align: center; } #fenye ul li:first-child{ border-top-left-radius:5px; border-bottom-left-radius:5px; } #fenye ul li:last-child{ border-top-right-radius:5px; border-bottom-right-radius:5px; border-right: 1px solid #c4c4c4;; } #fenye ul li{ border-left:1px solid #c4c4c4; border-bottom:1px solid #c4c4c4; border-top:1px solid #c4c4c4; background-color:#ffffff; width: 36px;px; font-size: 14px; list-style-type: none; display: inline-block; cursor: pointer; } #fenye ul li:nth-child(1),#fenye ul li:nth-child(2),#fenye ul li:nth-last-child(1),#fenye ul li:nth-last-child(2){ width: 50px; }
分页的js文件
$(function(){ var form = $("#form"); var fenye = $("#fenye"); //classpath的值 var classpath = $("#classpath").val(); //页面当前页面 var currentPage = $("#currentPage").val(); //页面总数 var totalPage = $("#totalPage").val(); //生成页面 if(currentPage>5){ if(parseInt(currentPage)+4>totalPage){ /* alert("当前页大于5且当前页+4超出总页数");*/ pageIcon(currentPage-5,totalPage); }else{ /* alert("当前页大于5当前页+4小于总页数");*/ pageIcon(currentPage-5,parseInt(currentPage)+4); } }else{ if(totalPage<10){ /* alert("当前页小于5总页数小于10");*/ pageIcon(1,totalPage); }else{ /* alert("当前页小于总页数大于10");*/ pageIcon(1,10); } } //根据当前选中页生成页面点击按钮 function pageIcon(startPage,endPage){ var ulHtml = "<li>首页</li><li>上一页</li>"; for(var i=startPage; i<=endPage; i++){ ulHtml += "<li>"+i+"</li>"; } ulHtml +="<li>下一页</li><li>末页</li>"; //为当前添加样式 $("#fenye ul").html(ulHtml); onClick(); } //遍历li function onClick(){ $("#fenye ul li").each(function(){ if($(this).html()=="首页"){ if(currentPage>1){ bindClick($(this),1); }else{ $(this).css({"color":"#c4c4c4"}); } } if($(this).html()=="末页"){ if(parseInt(currentPage)<totalPage){ bindClick($(this),totalPage); }else{ $(this).css({"color":"#c4c4c4"}); } } if($(this).html()=="上一页"){ if(currentPage>1){ bindClick($(this),parseInt(currentPage)-1); }else{ $(this).css({"color":"#c4c4c4"}); } } if($(this).html()=="下一页"){ if(parseInt(currentPage)<totalPage){ bindClick($(this),parseInt(currentPage)+1); }else{ $(this).css({"color":"#c4c4c4"}); } } if($(this).html()!="首页"&$(this).html()!="末页"& $(this).html()!="上一页"&$(this).html()!="下一页"& $(this).html()!=currentPage){ bindClick($(this),parseInt($(this).html())); } if($(this).html()==currentPage){ $(this).css({"background-color":"#efefef"}); } }); } function bindClick(obj,value){ obj.on("click",function(){ $("#currentPage").val(value); form.attr("action",classpath+"/getSystemconfig"); form.submit(); }); } });
dao中添加方法
public int getSystemconfigCount(); /*查询全部*/ public List<Systemconfig> getSystemconfigByAll(Map<String, Object> params);
service的方法
/*查询全部*/ public PageSupport<Systemconfig> getSystemconfigByAll(int pageSize,int currentPage);
service的实现方法
public PageSupport<Systemconfig> getSystemconfigByAll(int pageSize,int currentPage){ // TODO Auto-generated method stub int totalCount=0; int pageSize_temp=0; int currentPage_temp=0; int totalPage=0; //创建页面组件 PageSupport<Systemconfig> adPageSupport=new PageSupport<Systemconfig>(); //查询总记录 if((totalCount=systemconfigDao.getSystemconfigCount())>0){ //页面大小 if(pageSize<0){ pageSize_temp=1; }else if(pageSize>10){ pageSize_temp=10; }else{ pageSize_temp=pageSize; } //总页面 totalPage=(pageSize_temp+totalCount-1)/pageSize_temp; //当前页面 if(currentPage<0){ currentPage_temp=1; }else if(currentPage>totalPage){ currentPage_temp=totalPage; }else{ currentPage_temp=currentPage; } //创建查询条件 Map<String, Object> params = new HashMap<String, Object>(); params.put("startPage", currentPage_temp==1?0:currentPage_temp*pageSize_temp-1); params.put("pageSize", adPageSupport.getPageSize()); List<Systemconfig> lists=systemconfigDao.getSystemconfigByAll(params); //参数加入页面主键 adPageSupport.setCurrentPage(currentPage_temp); adPageSupport.setPageSize(pageSize_temp); adPageSupport.setTotalCount(totalCount); adPageSupport.setTotalPage(totalPage); adPageSupport.setResult(lists); adPageSupport.setResult(systemconfigDao.getSystemconfigByAll(params)); } return adPageSupport; }
action
private PageSupport<Systemconfig> systemconfigPageSupport; private int currentPage; public PageSupport<Systemconfig> getSystemconfigPageSupport() { return systemconfigPageSupport; } public void setSystemconfigPageSupport( PageSupport<Systemconfig> systemconfigPageSupport) { this.systemconfigPageSupport = systemconfigPageSupport; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public String getSystemconfig(){ systemconfigPageSupport = this.systemconfigService.getSystemconfigByAll(10, currentPage); ActionContext.getContext().put("systemconfigs", systemconfigPageSupport); return "success"; }
struts文件这里就省略了