您好,登錄后才能下訂單哦!
有時候需要根據條件查詢得出的數據較多,需要分頁顯示到頁面上。這時點擊下一頁就不方便每次帶查詢條件在數據庫中分頁。可以在list中進行分頁。
page的model類:
public class Page { private Integer currentPage;//當前頁 private int pageSize;//每頁顯示記錄條數 private int totalPage;//總頁數 private List<?> dataList;//每頁顯示的數據 private int star;//開始數據 public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<?> getDataList() { return dataList; } public void setDataList(List<?> dataList) { this.dataList = dataList; } public int getStar() { return star; } public void setStar(int star) { this.star = star; } }
控制層:
@RequestMapping("fenye.do") public String fen(Page page,HttpServletRequest request){ try { //查詢出的list數據 List<Data> showdata=loginService.selectyichall(); //剛開始的頁面為第一頁 if (page.getCurrentPage() == null){ page.setCurrentPage(1); } else { page.setCurrentPage(page.getCurrentPage()); } //設置每頁數據為十條 page.setPageSize(10); //每頁的開始數 page.setStar((page.getCurrentPage() - 1) * page.getPageSize()); //list的大小 int count = showdata.size(); //設置總頁數 page.setTotalPage(count % 10 == 0 ? count / 10 : count / 10 + 1); //對list進行截取 page.setDataList(showdata.subList(page.getStar(),count-page.getStar()>page.getPageSize()?page.getStar()+page.getPageSize():count)); //設置作用域 request.setAttribute("paging", page); return "add/show.jsp"; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "mistake.jsp"; } }
jsp頁面:
<script type="text/javascript" language="javascript"> function goPage(page){ location.href="fenye.do?currentPage=" rel="external nofollow" +page; } </script> <div> <table width="100%" border="0" align="center" cellspacing="1" > <c:forEach items="${paging.dataList }" var="upl"> <tr> <td align="center">${upl.name1 }</td> <td align="center">${upl.name2 }</td> <td align="center">${upl.name3 }</td> </tr> </c:forEach> </table> </div> <div id="venderfen"> <c:if test="${paging.totalPage > 0}"> 當前第 ${paging.currentPage } 頁/共 ${paging.totalPage} 頁 </c:if> <c:if test="${paging.totalPage > 1}"> <c:choose> <c:when test="${paging.currentPage==2 && paging.totalPage==2}"> <a onclick="goPage(1)">首頁</a> <a onclick="goPage(${paging.currentPage-1})">上一頁</a> </c:when> <c:when test="${paging.currentPage==1}"> <a onclick="goPage(${paging.currentPage+1})">下一頁</a> <a onclick="goPage(${paging.totalPage})">末頁</a> </c:when> <c:when test="${paging.currentPage==paging.totalPage}"> <a onclick="goPage(1)">首頁</a> <a onclick="goPage(${paging.currentPage-1})">上一頁</a> </c:when> <c:otherwise> <a onclick="goPage(1)">首頁</a> <a onclick="goPage(${paging.currentPage-1})">上一頁</a> <a onclick="goPage(${paging.currentPage+1})">下一頁</a> <a onclick="goPage(${paging.totalPage})">末頁</a> </c:otherwise> </c:choose> </c:if> 共 ${paging.total} 條 </div>
StringBuilder sb = new StringBuilder(); sb.append("SELECT distinct(m.time) "); sb.append(" from tbl_monitor m left join tbl_variable v on m.variable = v.id "); sb.append(" left join tbl_variable_group g on v.variable_group = g.id "); sb.append(" where v.active = true and g.active=true "); //sb.append(" AND m.variable in " + variableIds); sb.append(" AND exists ( select vtemp.id from tbl_variable vtemp where vtemp.selected=true and vtemp.id=m.variable)"); sb.append(" AND m.time between " + beginTime + " AND " + endTime ); sb.append(" order by m.time asc"); Query query = em.createNativeQuery(sb.toString()); //滿足搜索條件的所有的時間的條數 int total = query.getResultList().size(); tableView.setTotal(total); tableView.setTotalPage(total % page.getPageSize() == 0 ? total / page.getPageSize() : (total / page.getPageSize()) + 1); query.setFirstResult(from); query.setMaxResults(offset); timeList = query.getResultList();
以上就是本文關于java中對list分頁并顯示數據到頁面實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。