1. MySQL limit keyword (DAO)
Select * fromTablenameLimitStartpoint,Numberperpage;
Tablename The name of the table to be displayed by page;
StartpointIs the starting position-1;
NumberperpageIs the number of entries displayed on one page.
For example:Select * from comment Limit 20, 5;
Then 21 ~ is extracted from the comment table ~ Comment on the 25 th:
2. jquery load function (page JS)
The MySQL limit keyword can be used to extract records of a certain range (N, N + M]. That is to say, two parameters are required to determine the content displayed on a page, that is, "page x" and the number of entries displayed on each page.
The number of entries displayed on each page can be found inProgramCan also be set by the user. However, the "page X" parameter must be provided by the user. When a user clicks the page number, next page/Previous Page button, or jumps to a page, the "page X" parameter needs to be sent to the server for record extraction.
Function gotopage (PAGE ){
$ ('Body'). Load ("getcomments. do? Page = "+ page );
}
Alternatively, if both parameters are specified by the user, the function can be written:
Function gotopage (page, numberperpage ){
$ ('Body'). Load ("getcomments. do? Page = "+ Page +" & NPP = "+ numberperpage );
}
3. servlet receives parameters and organizes the content (Servlet File)
By accepting the page and NPP parameters in the request object sent from the JSP page, the servlet learns the page x you want to browse and the number of records displayed on the page.
Int page = integer. parseint (req. getparameter ("page "));
4. servlet computing page list
Generally, 10 pages are displayed at a time, that is, if the page number is 52nd, the available page lists are 50, 51, and 52... Until 60.
The calculation method is as follows: assume that the current page is on page X, the starting value is X/10*10, provided that x> 10. WriteCodeThat is:
Int start = 1;
If (page> = 10 ){
Start = page/10*10;
}
There are two special cases:
① A total of less than 10 pages
② The number of pages is not an integer multiple of 10
In this case, the page number list is smaller than 10, and it is easy to process. Just add the if condition to judge. The rough code is as follows:
Int Total = SM. getcommentcount (); Int Totalpage = total/ Itemsperpage; If (Total % itemsperpage! = 0 ) {Totalpage + = 1 ;} Vector <Integer> Pagearr =New Vector <integer> (); Int Start = 1 ; If (Page> = 10 ) {Start = Page/10*10 ;} Int Num = Start; While (! (Num> totalpage | num> Start + 10 ) {Pagearr. Add ( New INTEGER (Num )); ++Num ;}
5. display the page number list on the JSP page
4. We get a calculated page number list.Pagearr. In servletPagearrPut the list into the response object, and put the page (current page number) and totalpage (maximum page number) to help us make some judgments.
<! -- Previous page button -->
< Div ID = "Pagecontrol" > < C: Choose > < C: When Test = "$ {Page! = 1 }" > < A Href = "Checkcomments. do? Page =$ {page-1 }" > < Input Type = "Button" Name = "Lastpage" Value = "Previous Page" /></A> </ C: When > < C: otherwise > < Input Type = "Button" Disabled = "True" Name = "Lastpage" Value = "Previous Page" /><! -- To get the gray button --> </ C: otherwise > </ C: Choose >
<! -- Page list --> < C: foreach Items = "$ {Pagelist }" VaR = "Item" > < C: Choose > < C: When Test = "$ {Item = page }" > < A Href = "Checkcomments. do? Page =$ {item }" Class = "Currentpage" > $ {Item} </ A > </ C: When > < C: otherwise > < A Href = "Checkcomments. do? Page =$ {item }" > $ {Item} </ A > </ C: otherwise > </ C: Choose > </ C: foreach >
<! -- Next page button --> < C: Choose > < C: When Test = "$ {Page! = Totalpages }" > < A Href = "Checkcomments. do? Page =$ {page + 1 }" > < Input Type = "Button" Name = "Nextpage" Value = "Next page" /> </ A > </ C: When > < C: otherwise > < Input Type = "Button" Disabled = True Name = "Nextpage" Value = "Next page" /><! -- To get the gray button --> </ C: otherwise > </ C: Choose >
<! -- Jump directly -->$ {Totalpages} pages in total- < Input Type = "Text" ID = "Jumpto" /> Page < Input Type = "Button" Value = "Jump" Onclick = "Jumpto ($ {totalpages })" /> </ Div >
JS functions used
FunctionJumpto (maxpage ){VaRPage = $ ("# jumpto"). Val ();If(Page> maxpage | PAGE <1) {Alert ("Sorry, this page cannot be reached")}Else{$ ('Body'). Load ('checkcomments. do? Page = '+Page );}}
6. CSS Enhancement
To highlight the current page number, we made a special judgment in the above Code:
<C: WhenTest= "$ {Item = page }"><AHref= "Checkcomments. do? Page =$ {item }"Class= "Currentpage">$ {Item}</A></C: When>
In this way, the current page number will be marked as the currentpage class, so that you can emphasize it in the CSS file. For example:
. Currentpage {
Font-weight: bold;
Color: # ff9a00;
}
Or set the width of the following jump page input box
# Jumpto {
Width: 20px;
}
In this way, the current page will be marked in bold, orange:
7. Improvement
UseA tagAlthough it is convenient to do the link, but there will be an underline, it feels very uncomfortable. You can use CSS to remove it, or add some changes to the hover.
# Pagecontrol {
Text-Decoration: none;
}