Design and implementation of pagination in Easyjweb tools business engine

Source: Internet
Author: User
Tags array count implement integer interface query string access
web| page | Design in Web application development, whether there is no database, often use the paging problem. Easyjweb introduces the Ipagelist interface to easily solve the various paging problems we encounter, including pagination of database records, file directory paging, array or Java object paging.

Easyjweb as a web framework, the MVC core itself does not contain pagination content, and the paging design we call here refers to the design of the application of paging requirements in the Easyjweb tools business engine.

   1, the application of sample code

First take a look at the pagination design for the application sample code, the complete code for the example can be http://www.easyjf.com/download.htmDownload in!

   Sample Code A:Com.easyjweb.action.userManageAction.java

This is part of the code for the record paging display in Example 3 (Tim) in the Easyjweb document:
public class Usermanageaction extends Abstractcrudaction {
Public ipagelist doquery (webform form, int currentpage, int pageSize) {
....

Dbpagelist plist=new dbpagelist (User.class,scope,paras);//Returns the paging result by calling the Dbpagelist object ipagelist
Plist.dolist (currentpage,pagesize);
return plist;
}
...

As we can see from the code, this is an example of the paging processing of a log library Recordset object. Directly through the implementation of the Ipagelist interface Dbpagelist class implementation of the database paging.


   Sample Code B:Net.meybo.mail.action.EmailAction.java

This is the code for paging the message subject in the open source simplified version of the Meybomail Web mail client.
public class Emailaction implements Iwebaction {
...
Private Page dolist (WebForm form, Module module,activeuser user)
{
...
List List=null;
...
List=emailmanage.getmaillist (User.getusername (), User.getserverdomain (), boxname);
Ipagelist plist=new pagelist (new Listquery (list));
if (plist!=null) {
Plist.dolist (Pagesize,currentpage, "", "");
Form.addresult ("List", Plist.getresult ());
Form.addresult ("pages", New Integer (Plist.getpages ()));
Form.addresult ("Rows", New Integer (Plist.getrowcount ()));
Form.addresult ("page", New Integer (Plist.getcurrentpage ()));
Form.addresult ("gotopagehtml", Commutil.showpagehtml (Plist.getcurrentpage (), Plist.getpages ()));
}
...

The example above is paging through a list collection because the database is not used in the Meybomail Web, so the Listquery query processor is used for processing.

   

2, Easyjweb tools in the business engine related to paging interface and class

The paging problem is abstracted in Easyjweb tools by using the Ipagelist and IQuery two interfaces.
The following is the entire code for the Ipagelist interface:
  
Package com.easyjf.web.tools;
Import java.util.Collection;
Import java.util.List;
Public interface Ipagelist {
Public List GetResult ()//Retrieve the results of pagination
public void Setquery (IQuery q);/Set query processor
public int getpages ()//Return total pages
public int GetRowCount ()//Returns the total number of records
public int getcurrentpage ()//Return to current page
public void dolist (int pagesize,int pageno,string totalsql,string queryhql)//Paging processing
public void dolist (int pagesize,int pageno,string totalsql,string queryhql,collection paravalues);//Paging processing
}

In Ipagelist, we see the query and paging through the Setup query processor to implement the data, and here we look at the contents of the IQuery interface:

Package com.easyjf.web.tools;
Import java.util.Collection;
Import java.util.List;
Public interface IQuery {
int getRows (String conditing)//Total record number
List GetResult (String conditing)//query and return results based on criteria
void Setfirstresult (int begin);//Set start record
void setmaxresults (int max);//Set the maximum record returned per query
void Setparavalues (Collection paravalues);//Set query parameter values
List GetResult (String conditing,int begin,int max);//From the beginning of the result set begin, take Max record
}

This shows that our ipagelist is actually through the set call different query processor implementation of different types of data paging processing.


3, universal paging processing Ipagelist implementation PageList class
In Easyjweb tools, our PageList class implements the Ipagelist interface, a common paging class, which can be implemented by inheriting other types of data.

The full code for Pagelist.java is as follows:

Package com.easyjf.web.tools;

Import java.util.*;
/**
* Implementation of paging processing by calling IQuery
* @author Cai Shiyu
*
*/
public class PageList implements ipagelist{
private int rowcount;//record number
private int pages;//Total pages
private int currentpage;//actual pages
Private List result;
Private IQuery query;
Public PageList ()
{

}
Public pagelist (IQuery q)
{
this.query=q;
}
public void Setquery (IQuery q)
{
query=q;
}
Public List GetResult ()
{
return result;
}

public void dolist (int pageSize, int pageno, string totalsql, String queryhql) {
List Rs=null;
int total=query.getrows (TOTALSQL);
if (total>0) {
This.rowcount=total;
this.pages= (This.rowcount + pageSize-1)/pageSize; Count Total Pages
int intpageno= (Pageno>this.pages?this.pages:pageno);
if (intpageno<1) intpageno=1;
This.currentpage=intpageno;
if (pagesize>0) {
Query.setfirstresult ((intPageNo-1) * pageSize);
Query.setmaxresults (pageSize);
}
Rs=query.getresult (QUERYHQL);
}
Result=rs;
}
public void dolist (int pageSize, int pageno, string totalsql, String queryhql,collection paravalues) {
List Rs=null;
Query.setparavalues (paravalues);
int total=query.getrows (TOTALSQL);
if (total>0) {
This.rowcount=total;
this.pages= (This.rowcount + pageSize-1)/pageSize; Count Total Pages
int intpageno= (Pageno>this.pages?this.pages:pageno);
if (intpageno<1) intpageno=1;
This.currentpage=intpageno;
if (pagesize>0) {
Query.setfirstresult ((intPageNo-1) * pageSize);
Query.setmaxresults (pageSize);
}
Rs=query.getresult (QUERYHQL);
}
Result=rs;
}
public int getpages () {
return pages;
}
public int GetRowCount () {
return rowcount;
}
public int getcurrentpage () {
return currentpage;
}
}



4, the use of Hibernate access to the database IQuery interface implementation
This is the EASYJF Web site background, the use of Hibernate middleware to access the database, its corresponding query processor IQuery interface implementation.

All code for Dbquery.java:

Package Com.easyjf.comm;

Import java.util.Collection;
Import java.util.List;

Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Com.easyjf.web.tools.IQuery;
public class Dbquery implements IQuery {
Private session session;
private int begin;
private int Max;
Private List paravalues;
Public Dbquery (Sessions session)
{
This.session=session;
}

public void Setparavalues (Collection paravalues) {
This.paravalues= (List) paravalues;
SYSTEM.OUT.PRINTLN ("Parameters":p aravalues.size ();)
}

public int getRows (String conditing) {
Query query1=session.createquery (conditing);
if (paravalues!=null) {
for (int i=0;i<paravalues.size (); i++)
{
Query1.setparameter (I,paravalues.get (i));

}
}
int total= ((Integer) Query1.uniqueresult ()). Intvalue ();
return total;
}

Public List GetResult (String conditing) {
Query query=session.createquery (conditing);
if (paravalues!=null) {
for (int i=0;i<paravalues.size (); i++)
{
Query.setparameter (I,paravalues.get (i));
}
}
if (begin>0) query.setfirstresult (begin);
if (max>0) query.setmaxresults (max);
return Query.list ();
}

public void Setfirstresult (int begin) {
This.begin=begin;
}

public void setmaxresults (int max) {
This.max=max;
}

Public List GetResult (String conditing, int begin, int max) {
Query query=session.createquery (conditing);
if (begin>0) query.setfirstresult (begin);
if (max>0) query.setmaxresults (max);
return Query.list ();
}
public void Setparavalues (List paravalues) {
This.paravalues=paravalues;
}
}

5, for list data paging query processor Listquery implementation

When we want to page the target data is not a database record, but other storage methods (such as text, XML or in-memory real-time object threads, etc.), can be converted to an array or list. You can then implement a paging query by using the Listquery query processor.

All code for Listquery.java:

Package com.easyjf.web.tools;

Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.List;

public class Listquery implements IQuery {
private int begin=0;
private int max=0;
Private List List=null;
Public Listquery ()
{

}
Public Listquery (List l)
{
if (l!=null) {
This.list=l;
This.max=l.size ();
}
}
public void Initlist (List l)
{
This.list=l;
This.max=l.size ();
}
public int getRows (String conditing) {

Return (List==null?0:list.size ());
}

Public List GetResult (String conditing) {
Return List.sublist (Begin,begin+max>list.size () list.size (): Begin+max);
}
public void Setfirstresult (int begin) {
This.begin=list.size () <begin?list.size (): Begin;
}
public void setmaxresults (int max) {
This.max=max;
}
Public List GetResult (String conditing, int begin, int max) {

return list;
}
public void Setparavalues (Collection paravalues) {
TODO auto-generated Method Stub
}
}

6. Discussion on the algorithm of paging
Because the Easyjweb tools business Engine is a simple abstraction of many paging requirements. Therefore, the paging query processor algorithm can not be specific restrictions, the above two query processor is just a simple application example, we can according to the actual application of the requirements of the Project query processor implementation and algorithm design.
  
Because the level is limited, the design has a lot of unreasonable places, I implore you to correct me!


7, Easyjweb introduction

   Easyjwebis based on Java technology, applied to the rapid development of Web applications MVC Framework, framework design concept from many domestic project practice, fully borrow the current mainstream open source web framework (Struts, JSF, Tapestry, webwork, etc.), absorbing its advantages and essence, Using velocity as the template page engine, it is an MVC development framework that realizes the complete separation of page and code, and is a simple web framework designed to provide fast development practice for small and medium Web application systems.

The EASYJF Open source team began building in early 2006, so the current development team and the release of the work, are extremely immature. Easyjweb is still in the testing phase, please the vast number of Java enthusiasts a lot of criticism and suggestions. Also welcome you to join in our domestic open source team.

Easyjweb Official website: www.easyjf.com
EASYJF Team Official Website: www.easyjf.com

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.