package cn.edu.hpu.tax.core.page; import java.util.ArrayList; import java.util.List; public class PageResult { //总记录数 private long totalCount; //当前页号 private int pageNo; //总页数 private int totalPageCount; //页大小 private int pageSize; //列表记录 private List items; public PageResult(){ } //计算总页数 public PageResult(long totalCount, int pageNo, int pageSize, List items) { super(); this.totalCount = totalCount; this.pageNo = pageNo; this.pageSize = pageSize; this.items = items==null?new ArrayList():items; if(totalCount!=0){ //计算总数 int tem=(int)totalCount/pageSize; this.totalPageCount=(totalCount%pageSize==0)?tem:(tem+1); }else{ this.pageNo=0; } } public long getTotalCount() { return totalCount; } public void setTotalCount(long totalCount) { this.totalCount = totalCount; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getTotalPageCount() { return totalPageCount; } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List getItems() { return items; } public void setItems(List items) { this.items = items; } }
//分页对象 private PageResult pageResult; //页号 private int pageNo; //页大小 private int pageSize; public PageResult getPageResult() { return pageResult; } public void setPageResult(PageResult pageResult) { this.pageResult = pageResult; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { //给的默认的分页大小 if(pageSize < 1) pageSize = 4; return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; }
//列表页面 public String listUI() throws Exception{ try { //加载分类集合 ActionContext.getContext().getContextMap().put("infoTypeMap", Info.INFO_TYPE_MAP); QueryHelper queryHelper=new QueryHelper(Info.class,"i"); if(info != null){ if(StringUtils.isNotBlank(info.getTitle())){ info.setTitle(URLDecoder.decode(info.getTitle(),"utf-8")); queryHelper.addCondition("i.title like ?", "%"+info.getTitle()+"%"); } queryHelper.addCondition("i.state = ?", "1"); } //根据创建时间降序排序 queryHelper.addOrderByProperty("i.createTime", queryHelper.ORDER_BY_DESC); pageResult=infoService.getPageResult(queryHelper,getPageNo(),getPageSize()); } catch (Exception e) { throw new Exception(e.getMessage()); } return "listUI"; }
//分页 public PageResult getPageResult(QueryHelper queryHelper, int pageNo,int pageSize);
@Override public PageResult getPageResult(QueryHelper queryHelper, int pageNo, int pageSize) { return baseDao.getPageResult(queryHelper,pageNo,pageSize); }
//分页 public PageResult getPageResult(QueryHelper queryHelper, int pageNo,int pageSize);
@Override public PageResult getPageResult(QueryHelper queryHelper, int pageNo, int pageSize) { Query query=getSession().createQuery(queryHelper.getQueryListHql()); List<Object> parameters=queryHelper.getParameters(); //添加查询条件 if(parameters!=null){ for (int i = 0; i < parameters.size(); i++) { query.setParameter(i, parameters.get(i)); } } //进行分页 if(pageNo<1) pageNo=1; query.setFirstResult((pageNo-1)*pageSize);//设置数据起始索引号 query.setMaxResults(pageSize); List items=query.list(); //获取总记录数 Query queryCount=getSession().createQuery(queryHelper.getQueryCountHql()); if(parameters!=null){ for (int i = 0; i < parameters.size(); i++) { queryCount.setParameter(i, parameters.get(i)); } } long totalCount=(Long)queryCount.uniqueResult(); return new PageResult(totalCount, pageNo, pageSize, items); }
//查询统计数的hql语句 public String getQueryCountHql(){ return "SELECT COUNT(*) "+fromClause+whereClause; }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <%@include file="/common/header.jsp"%> <title>信息发布管理</title> <script type="text/javascript"> //全选、全反选 function doSelectAll(){ // jquery 1.6 前 //$("input[name=selectedRow]").attr("checked", $("#selAll").is(":checked")); //prop jquery 1.6+建议使用 $("input[name=selectedRow]").prop("checked", $("#selAll").is(":checked")); } //新增 function doAdd(){ document.forms[0].action = "${basePath}tax/info_addUI.action"; document.forms[0].submit(); } //编辑 function doEdit(id){ document.forms[0].action = "${basePath}tax/info_editUI.action?info.infoId=" + id; document.forms[0].submit(); } //删除 function doDelete(id){ document.forms[0].action = "${basePath}tax/info_delete.action?info.infoId=" + id; document.forms[0].submit(); } //批量删除 function doDeleteAll(){ document.forms[0].action = "${basePath}tax/info_deleteSelected.action"; document.forms[0].submit(); } //异步发布信息,信息的id以及将要改成的状态值 function doPublic(infoId,state){ //1.更新信息状态 $.ajax({ url:"${basePath}tax/info_publicInfo.action", data:{"info.infoId":infoId,"info.state":state}, type:"post", success:function(msg){ //2.更新状态栏,操作栏的显示值 if("更新状态成功"==msg){ if(state==1){ //说明信息状态已经被改成发布,操作栏被改为停用 $("#show_"+infoId).html("发布"); $("#oper_"+infoId).html('<a href="javascript:doPublic(\''+infoId+'\',0)">停用</a>'); }else{ $("#show_"+infoId).html("停用"); $("#oper_"+infoId).html('<a href="javascript:doPublic(\''+infoId+'\',1)">发布</a>'); } }else{ alert("更新信息状态失败!"); } }, error:function(){ alert("更新信息状态失败!"); } }); } function doSearch(){ document.forms[0].action = "${basePath}tax/info_listUI.action"; document.forms[0].submit(); } </script> </head> <body class="rightBody"> <form name="form1" action="" method="post"> <div class="p_d_1"> <div class="p_d_1_1"> <div class="content_info"> <div class="c_crumbs"><div><b></b><strong>信息发布管理</strong></div> </div> <div class="search_art"> <li> 信息标题:<s:textfield name="info.title" cssClass="s_text" id="infoTitle" cssStyle="width:160px;"/> </li> <li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li> <li style="float:right;"> <input type="button" value="新增" class="s_button" onclick="doAdd()"/> <input type="button" value="删除" class="s_button" onclick="doDeleteAll()"/> </li> </div> <div class="t_list" style="margin:0px; border:0px none;"> <table width="100%" border="0"> <tr class="t_tit"> <td width="30" align="center"><input type="checkbox" id="selAll" onclick="doSelectAll()" /></td> <td align="center">信息标题</td> <td width="120" align="center">信息分类</td> <td width="120" align="center">创建人</td> <td width="140" align="center">创建时间</td> <td width="80" align="center">状态</td> <td width="120" align="center">操作</td> </tr> <s:iterator value="pageResult.items" status="st"> <tr <s:if test="#st.odd"> bgcolor="f8f8f8" </s:if> > <td align="center"><input type="checkbox" name="selectedRow" value="<s:property value='infoId'/>"/></td> <td align="center"><s:property value="title"/></td> <td align="center"> <s:property value="#infoTypeMap[type]"/> </td> <td align="center"><s:property value="creator"/></td> <td align="center"><s:date name="createTime" format="yyyy-MM-dd HH:mm"/></td> <td id="show_<s:property value='infoId'/>" align="center"><s:property value="state==1?'发布':'停用'"/></td> <td align="center"> <span id="oper_<s:property value='infoId'/>"> <s:if test="state==1"> <a href="javascript:doPublic('<s:property value='infoId'/>',0)">停用</a> </s:if><s:else> <a href="javascript:doPublic('<s:property value='infoId'/>',1)">发布</a> </s:else> </span> <a href="javascript:doEdit('<s:property value='infoId'/>')">编辑</a> <a href="javascript:doDelete('<s:property value='infoId'/>')">删除</a> </td> </tr> </s:iterator> </table> </div> </div> <div class="c_pate" style="margin-top: 5px;"> <!-- 如果页数为空不显示分页选项 --> <s:if test="pageResult.totalCount > 0"> <table width="100%" class="pageDown" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="right"> 总共<s:property value="pageResult.totalCount"/>条记录,当前第 <s:property value="pageResult.pageNo"/> 页, 共 <s:property value="pageResult.totalPageCount"/> 页 <!-- 非第一页才有“上一页”选项 --> <s:if test="pageResult.pageNo>1"> <a href="javascript:doGoPage(<s:property value='pageResult.pageNo-1'/>)">上一页</a> </s:if> <!-- 非最后一页才有“下一页”选项 --> <s:if test="pageResult.pageNo < pageResult.totalPageCount"> <a href="javascript:doGoPage(<s:property value='pageResult.pageNo+1'/>)">下一页</a> </s:if> 到 <input id="pageNo" name="pageNo" type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1" max="" value="<s:property value="pageResult.pageNo"/>" /> </td> </tr> </table> </s:if><s:else>暂无数据!</s:else> </div> <script type="text/javascript"> //翻页方法 function doGoPage(pageNo){ document.getElementById("pageNo").value = pageNo; document.forms[0].action="${basePath}tax/info_listUI.action"; document.forms[0].submit(); } </script> </div> </div> </form> </body> </html>
至此我们的分页功能完成。下一次我们进行优化
转载请注明出处:http://blog.csdn.net/acmman/article/details/49942621