Realization of paging function of Java Web (11)

Although there are many easy-to-use frameworks to support paging, and it is very simple to make the paging effect, what is the process if you write your own handwriting? Let's talk about it today. Manually realize the paging effect.

                            --WZY

1、 Pagination idea

First of all, we need to know the thinking when writing paging code, keep the thinking clear and carry out it step by step, so as to be like water. Let's first look at the effect of paging

This is a paging navigation, in which the data you can get is

Totalrecord: the total number of employees and the total number of records in the database. There are 55 records here

Totalpage: total pages, 11 pages

PageSize: the number of records displayed on each page. Here you can see that 5 records are displayed on each page

Pagenum: what page is the current page? For example, page 9 in the figure, because 9 has no hyperlink,

Start: a total of 5 pages can be displayed for users to click, and 7 is the start page

End: 11 is the last page that can be displayed, that is, if the user clicks page 8, the start is 6 and the end is 10. There are only 5 pages each time. Click to query.

Each time, we can get five pieces of data required for the corresponding number of pages. These data should be displayed in JSP, that is, we have to get so much data from the background for display every time, so we try to encapsulate these data in a JavaBean, and each time the background puts the queried data into the JavaBean object, We only need to store the object in the request scope, and then get the required data from the domain in the JSP page.

2、 Create pagebean to store data

       PageBean. java

A total of 8 attributes are required: pagenum, PageSize, totalrecord, totalpage, StartIndex, list, start and end,

Pagenum, PageSize, totalrecord: it can be obtained through the construction method. Pagenum requests the parameters submitted by the page. PageSize is set by itself, and totalrecord is obtained by querying the database

Totalpage, StartIndex, start and end are obtained through internal algorithms,

The list needs to be obtained by querying the database through set.

Note: this class uses generics not only for this project, but also for other projects,

The code is as follows

3、 Write business logic code in the service layer

In fact, the pagebean object we need is built in this layer and returned to the previous layer

The user class is the encapsulated java bean of the data we need to display.  

4、 Write control code in Servlet

5、 Display data in JSP and build paging navigation

Because all the data we need is encapsulated in pagebean, and the pagebean object is in the request field, in the JSP page, we only need to get the data we need and display it. One thing to pay attention to in constructing the navigation map is that the logic should be clear. We can control what we want to display and what we don't want to display in the full screen, Just remember that when requesting a servlet, you need to give the requested page number to the server. Otherwise, the server doesn't know which page you want to get the data.

The logic code of the navigation map I made

Displays the number of all employees and the total number of pages

First, hyperlink

If the current page is the first page, the hyperlink of the previous page will not be displayed

If the current page is neither the first nor the last page, the previous page and the next page are displayed as hyperlinks

If the current page is the last page, only the hyperlink of the previous page will be displayed, and the next page will not be displayed

Footer hyperlink

Code

6、 Summary

In fact, pagination is really simple. The difficulty lies in one place. As long as you understand what attributes are required in pagebean and what the functions of various attributes are, pagination is so easy. Another is to write the logic of paging navigation in JSP. Don't be confused. In fact, it's not difficult at all. Interested students can realize the paging function by themselves. It is very helpful for me to understand paging. In the future, I don't have to copy and paste other people's paging code, and I can write it myself. Live on your own.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>