新聞列表的分頁查詢java代碼實現(xiàn)
更新時間:2016年08月13日 09:57:42 作者:夢牽繞了誰的靈魂
這篇文章主要為大家詳細介紹了新聞列表的分頁查詢java代碼實現(xiàn),感興趣的小伙伴們可以參考一下
本文實例為大家分享了新聞列表分頁查詢的java代碼,供大家參考,具體內(nèi)容如下
package com.ibeifeng.test; //創(chuàng)建新聞測試類 public class newTest { private long id; private String title; private String content; private String author; public newTest() { super(); } public newTest(long id, String title, String content, String author) { this.id = id; this.title = title; this.content = content; this.author = author; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "newTest [id=" + id + ", title=" + title + ", content=" + content + ", author=" + author + "]"; } } 2.開始查詢 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.ibeifeng.test.newTest"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <% List<newTest> list = new ArrayList<newTest>(107);//設定新聞行數(shù)為107行 for (int i = 1; i <= 107; i++) {//list中添加新聞 newTest news = new newTest(0L + i, i + "里約奧運", "馬龍獲得金牌-世界乒壇第五位男子“大滿貫”得主", "福音"); list.add(news); }//end of for...添加107條數(shù)據(jù)到集合中 //int pageIndex=10; int iTitleIndex = list.size();//獲取集合下表標 int iTitlePages = iTitleIndex / 10 + (iTitleIndex % 10 == 0 ? 0 : 1);//獲取頁數(shù)的總數(shù) int ipage = 4;//開始的頁數(shù) String str = request.getParameter("page"); if (str != null && !str.trim().equals("")) { int newPage = Integer.valueOf(str); if (newPage < 1) { ipage = 1; } else if (newPage > iTitlePages) { ipage = iTitlePages; } else { ipage = newPage; } } //創(chuàng)建一個新的集合(大小每個頁面顯示的新聞總數(shù)) 將107條數(shù)據(jù)分別存儲到其中 List<newTest> listPage = new ArrayList<newTest>(10); int ipa = 10;//獲取循環(huán)體的循環(huán)次數(shù)//最后一頁只有七條數(shù)據(jù) if (ipage == iTitlePages) { //當當前頁數(shù)為最后一頁時,剩余幾行則循環(huán)體之執(zhí)行剩余的行的數(shù)次, ipa = list.size() - (iTitlePages - 1) * 10; } for (int i = 0; i < ipa; i++) { //i=0;獲取前十個數(shù)據(jù) 第一次循環(huán)時ipage=4 newTest arr = list.get(i + (ipage - 1) * 10); listPage.add(arr); } %> <html> <body> <table> <tr> <th>標題</th> <td>作者</td> <td>摘要</td> </tr> <% for (int i = 0; i < listPage.size(); i++) { //java代碼需要用<% %》保護起來否則會被當做web語句執(zhí)行 newTest temp = listPage.get(i); %> <tr> <td><%=temp.getTitle()%></td> <td><%=temp.getAuthor()%></td> <td><%=temp.getContent()%></td> </tr> <% }//end of for... %> </table> <% boolean bFirst = ipage == 1; boolean bLast = ipage == iTitlePages ; %> <% if (!bFirst) { %> <a href="test.jsp?page=<%=ipage - 1%>&totopage=11">上一頁</a> <% } %> <!-- 當跳轉到第一頁時不再顯示“上一頁”提交對話框,下同 --> <% if (!bLast) { %> <a href="test.jsp?page=<%=ipage + 1%>&totopage=11">下一頁</a> <% } %>第<%=ipage%>頁 共<%=iTitlePages%>頁 </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于UncategorizedSQLException異常處理方案
這篇文章主要介紹了基于UncategorizedSQLException異常處理方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12springboot默認文件緩存(easy-captcha?驗證碼)
這篇文章主要介紹了springboot的文件緩存(easy-captcha?驗證碼),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06Java使用cxf發(fā)布及調用webservice接口的方法詳解
今天小編就為大家分享一篇關于Java使用CXF發(fā)布及調用WebService接口的方法,文中通過代碼示例給大家介紹的非常詳細,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2023-08-08