Want to do paging first familiar with the database, we use mysql SQL Server later.
Look at the effect.
1. You should be able to connect to the database: paging mysql is simpler than SQL server,
Page mysql for example:
"select * from name limit "+m+","+n+""
; where m refers to the index at the beginning of the record, starting from m, and n refers to several records
2. Create a paging javabean
public class Page { private int totalPagecount=1;//PageCount private int pagesize=10;//Number of records displayed on each page; private int totalcount=0;//Total number of records; private int currpageno=1;//Current page number; List<Name> namesList;//Entity set public int getTotalPagecount() { return totalPagecount; } public void setTotalPagecount(int totalPagecount) { this.totalPagecount = totalPagecount; } public int getPagesize() { return pagesize; } public void setPagesize(int pagesize) { if(this.pagesize>0) this.pagesize = pagesize; } public int getTotalcount() { return totalcount; } public void setTotalcount(int totalcount) { if(totalcount>0) this.totalcount = totalcount; //Calculate total pages totalPagecount=totalcount%pagesize==0?(totalcount/pagesize) :totalcount/pagesize+1; } public int getCurrpageno() { return currpageno; } public void setCurrpageno(int currpageno) { this.currpageno = currpageno; } public List<Name> getNamesList() { return namesList; } public void setNamesList(List<Name> namesList) { this.namesList = namesList; } }
3. Create object entity
//My object has three records. You can create them according to your own database public class Name { private int id; private String name; private String img; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } }
4. How many pieces of data and displayed data should be checked from the database
4.1 query mysql for few records
select count(*) as tc from name
4.2 connect to the database to find data. If you do not connect to the database, see here Database connection
public class PageDao { private static ComboPooledDataSource datasource; private static Connection con; static { datasource=new ComboPooledDataSource(); try { con=datasource.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * Get the total number */ public int getTotalCount() { String sql="select count(*) as tc from name"; PreparedStatement ps; int totalcount = 0; try { ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()) { totalcount=rs.getInt("tc"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return totalcount; } /* * pagesize//Number of records displayed per page * pageno //Page number */ public List<Name> getPageNameList(int pageno,int pageSize) throws SQLException{ String sql="select * from name limit "+pageSize*(pageno-1)+","+pageSize+""; //From how many displays, how many displays PreparedStatement ps=con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); List <Name> listname = new ArrayList<>(); while(rs.next()) { Name name=new Name(); name.setId(rs.getInt("id")); name.setName(rs.getString("name")); name.setImg(rs.getString("image")); listname.add(name); } return listname; } }
5. Next, go to the servlet to remove the corresponding method, which has passed the object to the front. Also the face gets the value with an el expression.
import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import huomin.Dao.PageDao; import huomin.entity.Name; import huomin.entity.Page; @WebServlet("/NewFile") public class NewFileServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String currpageno = req.getParameter("page"); String pageSize = req.getParameter("pageSize"); Page p=new Page(); if(pageSize!=null|| pageSize!="") { p.setPagesize(Integer.parseInt(pageSize)); } System.out.println("3333333333333333333333333333=="+currpageno); if(currpageno==null||currpageno=="") { p.setCurrpageno(1); }else { p.setCurrpageno(Integer.parseInt(currpageno)); } PageDao pd=new PageDao(); System.out.println(pd.getTotalCount()); p.setTotalcount((pd.getTotalCount())); List<Name> pageList=new ArrayList<>(); try { pageList = pd.getPageNameList(p.getCurrpageno(), p.getPagesize()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated method stub req.setAttribute( "Page",p); req.setAttribute( "pageList",pageList); req.getRequestDispatcher("/NewFile.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); } }
6. The next step is to get it from the jsp. I also made it very simple. I don't like to do the front-end functions, which are basically implemented
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> <style type="text/css"> *{ margin: 0;padding: 0;} .main{ height: 600px; width: 1000px; } .sj{ height: 500px; width: 999.9px; } .fy{ height: 99.9px; width: 999.9px; } </style> </head> <body> <script type="text/javascript"> function to_page(pag){ if(pag){ var zys=${Page.totalPagecount} if(pag>0&&pag<=zys){ var input = document.getElementById("page"); input.value=pag; } } var form = document.getElementById('form'); form.submit(); } function pagee(data){ var thisoblect=data.value; var input = document.getElementById("page"); if(thisoblect>0&&thisoblect<=${Page.totalPagecount}){ input.value=thisoblect; } to_page(); } </script> <form id="form" action="NewFile" method="post" > <div class="main"> <div class="sj"> <table style="height: 100%;width: 100%"> <tr> <td>id</td> <td>name</td> <td>image</td> </tr> <c:forEach items="${pageList}" var="pageList"> <tr> <td>${pageList.id}</td> <td>${pageList.name}</td> <td>${pageList.img}</td> </tr> </c:forEach> </table> </div> <div class="fy"> //A total of [< b > ${page. Totalcount} < / b >] records, [< b > ${page. Totalpagecount} < / b >] pages, ,Display per page <select onchange="to_page()" name="pageSize" id="pageSize"> <option value="30" <c:if test="${Page.pagesize==30}">selected</c:if>>30</option> <option value="10" <c:if test="${Page.pagesize==10}">selected</c:if>>10</option> </select> //strip //Current page number < input readonly = "readonly" size = "3" name = "page" id = "page" type = "text" style = "border: 0" value = "${page. Currpageno}" > [<button style="border:0;background-color: ;" onclick="javascript:to_page(${Page.currpageno-1})">next page before</button>] <c:choose> <c:when test="${Page.totalPagecount<=5}"> <c:set var="begin" value="1"/> <c:set var="end" value="${Page.totalPagecount}"/> </c:when> <c:when test="${Page.totalPagecount>5}"> <c:if test="${Page.currpageno<=3}"> <c:set var="begin" value="1"/> <c:set var="end" value="5"/> </c:if> <c:if test="${Page.currpageno>3}"> <c:set var="begin" value="${Page.currpageno-2}"/> <c:if test="${Page.totalPagecount>Page.currpageno+2}"> <c:set var="end" value="${Page.currpageno+2}"/> </c:if> <c:if test="${Page.totalPagecount<=Page.currpageno+2}"> <c:set var="end" value="${Page.totalPagecount}"/> </c:if> </c:if> </c:when> </c:choose> <c:forEach begin="${begin}" end="${end}" var="v" > <input onclick="pagee(this)" readonly="readonly" size="3" type="text" style="border: 0" value="${v}"> </c:forEach> <c:if test="${end<Page.totalPagecount }"> <span>...</span> </c:if> [<button style="border:0;background-color: ;" onclick="javascript:to_page(${Page.currpageno+1})">Next page</button>] </div> </div> </form> </body> </html>
; this is the end of a page.