The internet age is actually the era of data, the construction of large-scale e-commerce system must involve a large number of data display, data paging is frequently encountered problems, if each programmer to consider data acquisition and processing of every detail, it will be a bad and confusing things, Similar to pagination display this kind of public characteristic control logic must be implemented in the Horizonal layer layer
This paper presents a reusable and easy to use JSP to deal with pagination display in a step-by-step way.
If the data corresponding to a variety of business logic entities is called "Entity data," then the paging logic encapsulates the "control data" that controls the entity data (these two statements are used below).
Let's first build a Pagecontrol object that encapsulates some of the key "control data" involved in paging.
The specific description is as follows: public int curpage; is currently the first few pages of public int maxpage; A total number of pages public int maxrowcount; A total number of rows public int rowsperpage; How many rows per page are public yourdatatype yourdata;//Load data per page
The carrier of the "Entity data" to be displayed on each page is implemented in a variety of ways, such as in the form of a bean in the IBM e-commerce system MPE, an object-oriented implementation, The simple implementation of comparison can be java.util.Vector and so on, in order to avoid distracting attention to the core issues, this is abstracted with Youdatatype.
This.rowsperpage in fact should be obtained from the configuration file, the advantage is that the program can be read in the runtime to achieve dynamic (and then) configuration, the simple way is to write directly in the program. Public Pagecontrol (Yourpersistencelayer YOURPL)
This is a constructor with a parameter type of Yourpersistencelayer. Persistencelayer is a layer that deals directly with the database, and different companies have different implementations, such as Microsoft's ADO can be seen as a persistencelayer , IBM has also achieved a huge persistencelayer in its MPE system. One way to speculate is not to persistencelayer, or to dilute the layer, which is bound to reduce the stability, reusability and scalability of the system. You can refer to the appendix. In this constructor, there are several main actions:
About This.yourdata Here's another detail: when obtaining Entity data from a database, there are usually two ways: (A) to obtain all the data at once and (B) to discard the data on this page each time according to the current page number, taking into account that the data is often large or even massive, If a one-time acquisition, then this data is bound to occupy a large amount of server memory resources, so that system performance is greatly reduced, so the recommended method (A)
The rest of the work will be given to the servlet and JSP.
in the Service () method of the servlet, only the following actions are required:
Description: Yourbusinessobject encapsulates business logic, is an object located in business Logic layer, uses Ooad method, encapsulates business objects, and builds a solid persistent on layer business Logic layer is also the key to building a large e-business architecture. The focus of this article is only paging processing, not detailed discussion.
in each JSP page that wants to display data in a page turn, our work is simple, and the code is formulaic:
Xml:namespace prefix = jsp/> <%if (pagectl.maxpage!=1)) {%> <%@ include file= "/yourpath/pageman.jsp"% > <%}%> |
Description: if (pagectl.maxpage!=1) implements such a logic: if the data obtained is less than one page, then do not need to flip the display. We note that the <%@ include file= "/yourpath/pageman.jsp"%> this makes the real page-flipping part completely reusable.
So what did pageman.jsp do? it implements the familiar logic of people who often do page-flipping
(A) The first page cannot be flipped forward;
(B) The last page can not be backward;
can also make arbitrary jump on the page, the specific code as follows:
On the page will appear like the appearance of the following figure, as for the decoration beautification, it is the work of art.
Finally, a JavaScript common function for page jumps is attached:
If they are in a file named Turnpage.js, add such a reference to each JSP page that will display the paging data: