开发者

pagination in java?

i want the numbers to be displayed in this format..

1 2 3 4 5 ^

where if i press 5, then it should display 开发者_如何学JAVAfrom 5 to 10

5 6 7 8 9 10

till the max records are available. i just want to know a how to do this for displaying numbers.


Normally you want your database to do the heavy lifting for pagination and sorting. With MySQL for example you can get a page of results sorted by date by adding

ORDER BY date ASC LIMIT 5,5

to the end of your SQL query. If you use hibernate you can do this in a vendor-independent way using the criteria API:

List<MyDomainClass> results = (List<MyDomainClass>) getSession()     
.createCriteria(MyDomainClass.class)
.setFirstResult(5)
.setMaxResults(5)
.addOrder(Order.asc("date"))
.list();

To display a pagination navigation you also need to count the total number of results to know how many pages there are:

int count = (int) getSession()     
.createCriteria(MyDomainClass.class)
.setProjection(Projections.rowCount())
.list().get(0);

You can add restrictions to both searches, for example when you want to filter on lastname you could add:

.add(Restrictions.eq("lastname", "Smith")

(This needs to be added to both the count query and the list query).

When you know the total number of results, the pagesize and the current page number you can calculate the range of results like this :

// TODO: execute the count query here to determine totalResults
int totalPages = Math.ceil(totalResults / pageSize);
// show first page by default
int firstResult = 0;
if (currentPage >= 0 && currentPage < totalPages) {
    firstResult = currentPage * pageSize;    
}
// the number of items might be less than the page size (i.e. on the last page)
int count = Math.min(pageSize, totalResults - firstResult);        
// TODO: execute the list query here to get a page of data

How you display the navigation is up to you. Some frameworks have standard components for that. Otherwise you will have to think of a way to handle huge amounts of pages. A common approach is to show a range of say 10 page numbers and forward/backward jump to start/jump to end links.

Hope this helps.


We cannot give the specific/exact answer you expected. The thing here is we can give you some advice for some logic ways to accomplish your request.

First thing is, read some documents for creating a well designed structure of your pagination. Here are some links that may help you in building the design of your pagination.

  • Implementing Search Result Pagination in a Web Application
  • A Pagination Technique Using Spring
  • Pagination: Server Side or Client Side?

For my personal experience in designing my pagination and a large data will retrieve from a database. I'll use a LIMIT in my sql query to fasten retrieving the results. And I'll do things as follows.

  • First I do have a Model a class/object that will hold the pages that will display in my presentation layer.
  • The processing of query will process in a DAO/my business layer.
  • Now in my Services/Logic layer will process the assigning of values to my model. This where also the forwarding and requesting of data will be handle.
  • And in my presentation layer which the results of the query and the parameter for every links in pagination will display.

I know my explanation is not clear but I hope you get some point on it.

And now you could ask what's the relevant to your question. It is the process of how your presentation communication with your business layer. What I mean is the flow on how will you get the result you needed if some page number was call. Seems not really clear for that I'll make pseudo-codes that more related to you're question.

Since you don't have a ( First | Prev | page no here.... | Next | Last ) on your pagination. I'll make it short as possible.

First loading of the page with default pages. Assuming page data is from database and with the use of LIMIT

  1. DAO/Business - SELECT pagesContent/link FROM someTables LIMIT 0,5;..
  2. Services/Logic - Put links for page 1 and so on... and put it to pagination Model..
  3. Presentaton/Display - Display now the pages that stored in pagination Model.. Now in page5 or the last page you get the query you will pass another parameter that will change the starting limit of the query and you can pass also the nth page..

The logic here is, from your pagination display you will need to pass a value that your logic layer will understand what will be the next pages to be display. Another way is you can set a condition in you logic layer how to count the pages if it is not in the range of 1 - 4 or other thing you need to validate.

I hope this can be a help.


Last week i discovered primefaces, it has lots of cool tools and gadgets, here have a look http://www.primefaces.org/showcase/ui/datatableHome.jsf if what you do is desktop apps you can have a look at javaFX http://javafx.com/


If you have a List of records then you can do pagination using subList method of List.

List.subList(fromIndex, endIndex);

returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜