您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何實(shí)現(xiàn)一個(gè)通用的Java分頁(yè)基類(lèi)的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
分頁(yè)的基類(lèi)
import java.util.List; /** * 分頁(yè)顯示的標(biāo)準(zhǔn)類(lèi),基本操作,是先給予-當(dāng)前頁(yè)數(shù)一共的數(shù)據(jù)條數(shù)-每頁(yè)顯示的條數(shù), * 然后在初始化該類(lèi),得到總共頁(yè)數(shù),和開(kāi)始序號(hào)和結(jié)束序號(hào), * 然后數(shù)據(jù)庫(kù)分頁(yè)用到開(kāi)始序號(hào)和結(jié)束序號(hào),得到數(shù)據(jù)集合后賦值給該類(lèi)的list屬性, * * 然后把該類(lèi)發(fā)送到j(luò)sp頁(yè)面,進(jìn)行訪(fǎng)問(wèn) * @author admin * * @param <T> */ public class PageBean<T> { private int pageIndex; //當(dāng)前頁(yè)數(shù) private int pageSize; //一共的頁(yè)數(shù) private int count; //數(shù)據(jù)條數(shù) private int pageCount; //每頁(yè)的數(shù)據(jù)條數(shù) private int start; //起始數(shù)據(jù)位置 private int end; //結(jié)束 private List<T> list=null; public void init(){ /*根count 和pageCount計(jì)算頁(yè)數(shù)pageSize */ int pageSize_x=(int)count/pageCount; if(count>=pageCount){ this.pageSize=count%pageCount==0?pageSize_x:pageSize_x+1; } else{ this.pageSize=1; } //判斷頁(yè)數(shù)和當(dāng)前頁(yè)數(shù) if(pageIndex>pageSize){ pageIndex=pageSize; } if(pageIndex<1){ pageIndex=1; } //根據(jù)當(dāng)前頁(yè)計(jì)算起始和結(jié)束條目 this.start=(pageIndex-1)*pageCount+1; this.end=pageIndex*pageCount; } public PageBean(int pageIndex, int count, int pageCount) { super(); this.pageIndex = pageIndex; this.count = count; this.pageCount = pageCount; } public PageBean(int pageIndex, int count, int pageCount, List<T> list) { super(); this.pageIndex = pageIndex; this.count = count; this.pageCount = pageCount; this.list = list; } public PageBean() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "PageBean [count=" + count + ", end=" + end + ", list=" + list + ", pageCount=" + pageCount + ", pageIndex=" + pageIndex + ", pageSize=" + pageSize + ", start=" + start + "]"; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }
servlet調(diào)用
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.MessageDao; import com.dao.impl.MessageDaoImpl; import com.vo.Message; import com.vo.PageBean; public class ShowMessageServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 6646899131087204214L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); int pageIndex=0; MessageDao md=new MessageDaoImpl(); String pageIndexStr=req.getParameter("pageIndex"); if(pageIndexStr!=null){ try{ pageIndex=Integer.parseint(pageIndexStr); } catch (Exception e) { } } PageBean<Message> pb=new PageBean<Message>(pageIndex,md.getMessageCount(),10); pb.init(); pb.setList(md.getMessageListOfPage(pb.getStart(), pb.getEnd())); req.setAttribute("pagebean", pb); req.getRequestDispatcher("index.jsp").forward(req, resp); } }
jsp頁(yè)面的顯示調(diào)用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <c:if test="${empty pagebean}"> <jsp:forward page="showmessage"></jsp:forward> </c:if> <body> <c:forEach var="message" items="${pagebean.list}"> ${message.title } ${message.editdate }<br/> </c:forEach> <a href="showmessage?pageIndex=${pagebean.pageIndex+1}" rel="external nofollow" >下一個(gè)</a>[${pagebean.pageIndex }<span>/</span>${pagebean.pageSize}] </body> </html>
感謝各位的閱讀!關(guān)于“如何實(shí)現(xiàn)一個(gè)通用的Java分頁(yè)基類(lèi)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。