Calculate and determine the total number of pages of the data table: 101pageSize1001010. Rowsselectcount (1) fromusers; Method 1: pageCountrowspageSize + (rows % pageSize0? ); Method 2: pageCount (rows + (pageSize-1) (pageSize); publicvoiddoGet (HttpServletRequestreq,
The calculation determines the total number of pages of the data table: 101/pageSize = 100/10 = 10. Rows = select count (1) from users; Method 1: pageCount = rows/pageSize + (rows % pageSize = 0? ); Method 2: pageCount = (rows + (pageSize-1)/(pageSize); public void doGet (HttpServletRequest req,
The calculation determines the total number of pages of the data table: 101/pageSize = 100/10 = 10. Rows = select count (1) from users; Method 1: pageCount = rows/pageSize + (rows % pageSize = 0? 0: 1); Method 2: pageCount = (rows + (pageSize-1)/(pageSize); public void doGet (HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {// Step 1: define the number of rows displayed per page int pageSize = 10; try {// Step 2: Get the number of rows in the data table QueryRunner run = new QueryRunner (performanceutils. getDatasSource (); String SQL = "select count (*) from users"; Object o = run. query (SQL, new ScalarHandler (); int rows = Integer. parseInt (o . ToString (); // Step 3: calculate the total number of pages divided into int pageCount = rows/pageSize + (rows % pageSize = 0? ); // Place the number of pages in reqreq. setAttribute ("pageCount", pageCount);} catch (Exception e) {e. printStackTrace ();} // forward to req. getRequestDispatcher ("/jsps/show. jsp "). forward (req, response );}Step 4: analyze the start position of limit
User requested facial code |
Start |
Algorithm: |
1 |
Limit 0, pageSize |
Start = (CurrentPage-1) * pageSize |
2 |
Limit 10, pageSize |
3 |
Limit 20, pageSize |
Step 5: page after page
Each page can display up to 10 page numbers.
PageNum = 10;
StartNo
EndNo
Current page number |
Page number range |
Algorithm |
1 |
1 ~ 10 |
If (currentPage <= pageNum/2) 1 ~ 10 |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
2 ~ 11 |
6-4 = 2 = 6-(pageNum/2-1) = 2 = startNo EndNo = startNo + (pageNum-1) = 11 |
7 |
3 ~ 12 |
8 |
4 ~ 13 |
9 |
|
|
10 |
|
|
11 |
|
11-4 = 7 EndNo = 7 + 9 = 16 EndNo = 11; StartNo = 2 = endNo-(pageNum-1 ); |
|
|