The internet age is actually the data age. Building a large E-commerce system involves a large amount of data display. The paging display of data is a frequent problem, if every programmer needs to consider every detail of data acquisition and processing, it will be terrible and messy, the control logic similar to pagination must be implemented at the horizonal layer.
This article provides a step-by-step method for processing paging display with JSP, which is easy to transplant.
If the data corresponding to various business logic entities is called "physical data ", the paging display logic encapsulates "Control Data" that controls the object data (these two statements are used below ).
First, let's construct a pagecontrol object to encapsulate some key "Control Data" involved in paging.
The details are as follows:
- Public int curpage; // the current page
- Public int maxpage; // total number of pages
- Public int maxrowcount; // The total number of rows.
- Public int rowsperpage; // The number of rows on each page.
- Public yourdatatype yourdata; // load data on each page
There are various implementation methods for the carrier of "Entity Data" to be displayed on each page. For example, in the ibm e-commerce system MPE, it is in the form of bean, which is an object-oriented implementation, relatively simple implementation available Java. util. vector and so on. To avoid distracting attention to core issues, you can use youdatatype to abstract them here.
-
Public void countmaxpage () {// calculate the total number of pages based on the total number of rows if (this. maxrowcount % This. rowsperpage = 0) {This. maxpage = This. maxrowcount/This. rowsperpage;} else {This. maxpage = This. maxrowcount/This. rowsperpage + 1 ;}}
|
This. rowsperpage should actually be obtained from the configuration file. The advantage of this is that the program can be read in the running state to achieve dynamic (and then) configuration. The simple method is to write it directly in the program.
- Public pagecontrol (yourpersistencelayer yourpl)
This is a constructor whose parameter type is yourpersistencelayer. persistencelayer is a layer that deals directly with databases. Different companies have different implementations. For example, Microsoft's ADO can be seen as a persistencelayer, IBM has also implemented a large persistencelayer in its MPE system ,. A speculative approach is not to persistencelayer, or to dilute this layer. This will inevitably reduce system stability, reusability, and scalability. For details, refer to the document in the Appendix. In this constructor, there are several main operations:
This. maxrowcount = yourpl. getavailablecount (); // get the total number of rows this. yourdata = yourpl. getresult (); // obtain the data to be displayed on this page. countmaxpage (); // calculate the total number of pages
|
About this. yourdata has another details: When retrieving "Entity Data" from the database, there are usually two ways: (a) one-time acquisition of all data; (B) each time based on the current page number, obtain the data on this page and discard other data. Considering that the data is often massive or even massive, if the data is obtained at one time, the data must occupy a large amount of server memory resources, this greatly reduces system performance. Therefore, we recommend that you use ()
The following work can be handed over to Servlet and JSP.
In the servlet Service () method, you only need to perform the following operations:
PageControl pageCtl = yourBusinessObject.listData(req.getParameter("jumpPage"));req.setAttribute("pageCtl",pageCtl);
|
Note: yourbusinessobject encapsulates business logic. It is an object located in the business logic layer. It uses OOAD to encapsulate business objects, building a solid business logic layer on the persistent layer is also the key to building a large-scale e-commerce architecture. The focus of this article is only paging.
In each JSP page that you want to display data on pages,Our work is also very simple, and its code is formulated:
- Note:
- If (pagectl. maxpage! = 1) the logic is implemented: If the obtained data is less than one page, the page is not displayed.
- We noticed that this completely reused the real page turning part.
So what exactly does pageman. jsp do?It implements the familiar logic that often involves page turning.
(A) The first page cannot be pushed forward;
(B) The last page cannot be rolled back;
At the same time, you can jump to any page. The specific code is as follows:
Total lines per page Homepage previous page next page last page>>Page
|
On the page, the page will show a similar appearance. As for the decoration and beautification, it is the work of the artist.
The following is a javascript public function used for page Jump:
function Jumping(){ document.PageForm.submit(); return ;}function gotoPage(pagenum){ document.PageForm.jumpPage.value = pagenum; document.PageForm.submit(); return ;}
|
If they are located in a file named turnpage. JS, add the following reference to each JSP page to display paging data: