JavaBean + JSP + Dao implement Paging

Source: Internet
Author: User
Pagebean. Java

Package com. servlet;
Import java. util .*;
Public class pagebean {
Private list arraylist; // stores the total data
// Page information Definition
Private int totalrows = 0; // total data count
Private int pagerecorders = 20; // number of records per page
Private int totalpages = 0; // total number of pages
Private int pagestartrow = 0; // start number of each page
Private int pageendrow = 0; // The number of data termination records displayed on each page
Private int currentpage = 1; // current page
Private Boolean hasnextpage = false; // whether the next page exists
Private Boolean haspreviouspage = false; // whether the previous page exists
Public int getcurrentpage (){
Return currentpage;
}
Public void setcurrentpage (INT currentpage ){
This. currentpage = currentpage;
}
Public Boolean ishasnextpage (){
Return hasnextpage;
}
Public void sethasnextpage (Boolean hasnextpage ){
This. hasnextpage = hasnextpage;
}
Public Boolean ishaspreviouspage (){
Return haspreviouspage;
}
Public void sethaspreviouspage (Boolean haspreviouspage ){
This. haspreviouspage = haspreviouspage;
}
Public int getpageendrow (){
Return pageendrow;
}
Public void setpageendrow (INT pageendrow ){
This. pageendrow = pageendrow;
}
Public int getpagerecorders (){
Return pagerecorders;
}
Public void setpagerecorders (INT pagerecorders ){
This. pagerecorders = pagerecorders;
}
Public int getpagestartrow (){
Return pagestartrow;
}
Public void setpagestartrow (INT pagestartrow ){
This. pagestartrow = pagestartrow;
}
Public int gettotalpages (){
Return totalpages;
}
Public void settotalpages (INT totalpages ){
This. totalpages = totalpages;
}
Public int gettotalrows (){
Return totalrows;
}
Public void settotalrows (INT totalrows ){
This. totalrows = totalrows;
}

Public pagebean (list arraylist, int pagerecorders ){
This. arraylist = arraylist;
This. pagerecorders = pagerecorders;
Totalrows = arraylist. Size (); // obtain the total number of data
// Obtain the total number of pages
If (totalrows % pagerecorders) = 0 ){
Totalpages = totalrows/pagerecorders;
} Else {
Totalpages = totalrows/pagerecorders + 1;
}
Totalpages = (totalpages = 0 )? 1: totalpages;
// Set the start position and end position on the first page
If (totalrows <pagerecorders ){
This. pagestartrow = 0;
This. pageendrow = totalrows;
} Else {
This. pagestartrow = 0;
This. pageendrow = pagerecorders;
}
// Whether the next page exists
If (currentpage> = totalpages ){
Hasnextpage = false;
} Else {
Hasnextpage = true;
}
}
// Go to the page
Public list getpagelist (INT currentpage ){
// The current page is smaller than 1
If (currentpage <= 0 ){
Currentpage = 1;
}
// The current page is larger than the total number of pages
If (currentpage> = totalpages ){
Currentpage = totalpages;
Hasnextpage = false;
} Else {
Hasnextpage = true;
}
// The current page is greater than 1
If (currentpage> 1 ){
Haspreviouspage = true;
} Else {
Haspreviouspage = false;
}
// Determine whether it is the last page
If (currentpage * pagerecorders <totalrows ){
Pageendrow = currentpage * pagerecorders;
Pagestartrow = pageendrow-pagerecorders;
} Else {
Pageendrow = totalrows;
Pagestartrow = pagerecorders * (totalpages-1 );
}
List <Object> pagelist = new arraylist <Object> ();
For (INT I = pagestartrow; I <pageendrow; I ++ ){
Pagelist. Add (arraylist. Get (I ));
}
Return pagelist;
}
}

Myjsp. jsp

<% @ Page C %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "com. VO. *" %>
<% @ Page import = "com. servlet. pagebean" %>
<JSP: usebean id = "right" Scope = "request" class = "com. Factory. Factory"/>
<HTML>
<Head>
<Title> panda Eye Studio </title>
<LINK rel = "stylesheet" href = "CSS/shop.css" type = "text/CSS">
</Head>
<Body>
<%!
Int currentpage = 1;
Int recorders = 4;
%>
<%
If (request. getparameter ("page") = NULL | request. getparameter ("page") = "")
{
Currentpage = 1;
}
Else
{
Currentpage = integer. parseint (request. getparameter ("page "));
}
List shoplist = right. getshopinstance (). queryall (); // data queried by the database
Pagebean pages = new pagebean (shoplist, recorders); // you can specify the data to be paged and the number of records on each page.
Pages. setcurrentpage (currentpage); // you can specify the current page.
List all = pages. getpagelist (currentpage); // call the data on the current page
Iterator cart = all. iterator (); // Iteration
While (Cart. hasnext () // output data
{
Shopvo bv = (shopvo) cart. Next ();
String name = BV. getgoodsname ();
%>
<% = Name %>
<% }%>
<P>
<! -- Pagination navigation bar -->
<Form name = "form1" method = "Post" Action = "myjsp. jsp">
Total <% = pages. gettotalrows () %> entries <% = recorders %>/page <% = pages. getcurrentpage () %> page/total <% = pages. gettotalpages () %> page
<% IF (pages. getcurrentpage () <= 1) {%>
Homepage
<%} Else {%>
<A href = "myjsp. jsp? Page = 1 "> homepage </a>
<% }%>
<% IF (pages. ishaspreviouspage () {%>
<A href = "myjsp. jsp? Page = currentPage-1 %> "> previous </a>
<%} Else {%>
Previous Page
<% }%>
<% IF (pages. ishasnextpage () {%>
<A href = "myjsp. jsp? Page = <% = currentpage + 1%> "> next page </a>
<%} Else {%>
Next Page
<% }%>
<% IF (pages. getcurrentpage () <pages. gettotalpages () {%>
<A href = "myjsp. jsp? Page = <% = pages. gettotalpages () %> "> last page </a>
<%} Else {%>
Last page
<% }%>
<Select name = "page">
<% For (INT I = 1; I <= pages. gettotalpages (); I ++)
{%>
<Option value = "<% = I %>" <% if (I = pages. getcurrentpage () {out. println ("selected") ;}%><%= I %> </option>
<% }%>
</SELECT>
<Input type = "Submit" name = "Submit" value = "">
</Form>
</P>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.