A simple paging implementation (2)

xiaoxiao2021-03-06  63

Here, a simpler paging implementation is built, that is, by building a page object by a list of query results, this situation exists: such as this time to display, not directly from the database query, but by filtering a certain time The database query is obtained, in summary is a collection of results containing all data.

I don't know if there is any clear, here is directly given a reference implementation: package treeroot.util; import java.util.list;

/ *** @Author Treerot * @version 1.0 * @since 2004-9-30 * / public class listpage extends AbstractPage imports pageable {private list list;

/ ** * Initialize * @Param List * @throws pageException * / public listpage (list, 1, pageable.default_pageSize);} / ** * is coming initialization, this page designated * @param list * @param currentPage * @throws PageException * / public ListPage (List list, int currentPage) throws PageException {this (list, currentPage, Pageable.DEFAULT_PAGESIZE);} / ** * results through a list to initialize, this page and the page size designated * @param list * @param currentPage * @param pageSize * @throws PageException * / public ListPage (list list, int currentPage, int pageSize) throws PageException {super (currentPage, pageSize); / / Here, in order to achieve Fail-Fast, there is no need to throw an exception, not necessary. IF (list == null) throw new nullpointRexception (); this.list = list; init ();

protected void init () throws PageException {this.count = list.size (); checkPage (this.getCurrentPage ()); int fromIndex = (this.getCurrentPage () - 1) * this.getPageSize (); int toIndex = Math .min (fromNDEX THIS.GETPAGESIZE (), this.count); this.Result = list.sublist (fromNDEX, TOINDEX);

} Do you feel very simple, look at how it is applied, if you have a method to get the results of the query, then you can use it.

Servlet or in the Action: int currentPage = 1; String str = request.getParameter ( "currentPage"); if (str = null!) {Try {currentPage = Integer.parseInt (str);} catch (NumberFormatException e) {} } List list = .... // Get all results here, it may be directly obtained database query results, or it may be processed. Pageable pg = null; try {pg = new listpage (list, currentpage); // or through DAO PG = (New DAO ()); return type is pageable} catch (pageexception E) {pg = null } Request.setttribute ("Page", PG); // Forward to a JSP page display.

转载请注明原文地址:https://www.9cbs.com/read-111185.html

New Post(0)