A Practical JSP paging code

Source: Internet
Author: User
There is a page requesting action, the action calls the paging implementation class, and then an enthusiastic user replies to the displayed page:
Str + = "go ";I already tried it. No problem1. The following is a pagination class PageResultSet.Package page. bean; import java. util. *; public class PageResultSet {/***** paging data */private Collection data = null;/***** current page */private int curPage; /*** number of records per page */private int pageSize;/*** number of record lines */private int rowsCount;/*** page number */private int pageCount; public PageResultSet (Collection data) {this. data = data; this. curPage = 1; this. pageSize = 10; this. rowsCount = data. size (); this. pageCount = (Int) Math. ceil (double) rowsCount/pageSize);} public PageResultSet (Collection data, int curPage) {this. data = data; this. curPage = curPage; this. pageSize = 10; this. rowsCount = data. size (); this. pageCount = (int) Math. ceil (double) rowsCount/pageSize);} public PageResultSet (Collection data, int curPage, int pageSize) {this. data = data; this. curPage = curPage; this. pageSize = pageSize; t His. rowsCount = data. size (); this. pageCount = (int) Math. ceil (double) rowsCount/pageSize);}/*** getCurPage: returns the current page number ** @ return int */public int getCurPage () {return curPage ;} /*** getPageSize: return page size ** @ return int */public int getPageSize () {return pageSize;}/*** getRowsCount: total number of returned records ** @ return int */public int getRowsCount () {return rowsCount;}/*** getPageCount: Total number of returned records ** @ re Turn int */public int getPageCount () {return pageCount;}/*** first page ** @ return int */public int first () {return 1 ;} /*** last page ** @ return int */public int last () {return pageCount ;} /*** previous Page ** @ return int */public int previous () {return (curPage-1 <1 )? 1: curPage-1;}/*** next page ** @ return int */public int next () {return (curPage + 1> pageCount )? PageCount: curPage + 1;}/*** first page ** @ return boolean */public boolean isFirst () {return (curPage = 1 )? True: false;}/*** last page ** @ return boolean */public boolean isLast () {return (curPage = pageCount )? True: false;}/*** get the current page data ** @ return Collection */public Collection getData () {Collection curData = null; if (data! = Null) {int start = (curPage-1) * pageSize; int end = 0; if (start + pageSize> rowsCount) end = rowsCount; else end = start + pageSize; arrayList arrayCurData = new ArrayList (); ArrayList arrayData = null; Vector vectorCurData = new Vector (); Vector vectorData = null; boolean isArray = true; if (data instanceof ArrayList) {arrayData = (ArrayList) data; isArray = true;} else if (data instance Of Vector) {vectorData = (Vector) data; isArray = false ;}for (int I = start; I <end; I ++) {if (isArray) {arrayCurData. add (arrayData. get (I);} else {vectorData. add (vectorData. elementAt (I) ;}}if (isArray) {curData = (Collection) arrayCurData ;}else {curData = (Collection) vectorCurData ;}} return curData ;} /*** get toolbar ** @ return String */public String getToolBar (String fileName ){ String temp = ""; if (fileName. indexOf ("? ") =-1) {temp = "? ";}Else {temp =" & ";}string str =" "; return str ;}}2. Here is Action./** Generated by MyEclipse Struts * Template path: templates/java/JavaClass. vtl */package struts. action; import java. util. collection; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. struts. action. action; import org. apache. struts. action. actionForm; import org. apache. struts. action. actionForward; import org. apache. struts. action. actionMap Ping; import page. bean. databaseConn; import page. bean. pageResultSet; import page. bean. contactBO; import struts. form. loginForm;/*** MyEclipse Struts * Creation date: 04-02-2008 ** XDoclet definition: * @ struts. action path = "/login" name = "loginForm" input = "/login. jsp "scope =" request "validate =" true "* @ struts. action-forward name = "sss" path = "/index. jsp "*/public class LoginAction extends Action {/** Generated Methods * // *** Method execute * @ param mapping * @ param form * @ param request * @ param response * @ return ActionForward */public ActionForward execute (ActionMapping mapping, actionForm form, HttpServletRequest request, HttpServletResponse response) {LoginForm loginForm = (LoginForm) form; ContactBO userBO = new ContactBO (); // retrieve data (ArrayList or Vector format) from the business processing logic class first to collect data; try {Data = userBO. findContact (DatabaseConn. getConnection (); // obtain the current page curPage and the number of records per page pageSize // int curPage = Integer. parseInt (String) request. getParameter ("cur_page"); int curPage = 1; String cur = request. getParameter ("cur_page"); System. out. println ("--------------:" + cur); if (cur! = Null & cur! = "") {CurPage = new Integer (cur ). intValue () ;}int pageSize = 10; // then generate the PageResultSet object PageResultSet dataList = new PageResultSet (data, curPage, pageSize); request. setAttribute ("usersList", dataList); return mapping. findForward ("sss");} catch (Exception e) {e. printStackTrace (); return mapping. getInputForward ();}}}3. The following page is displayed.<% @ Page language = "java" import = "java. util. *, page. bean. contact, page. bean. pageResultSet "pageEncoding =" gb2312 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> My JSP 'index. jsp 'starting page <% PageResultSet pageResultSet = (PageResultSet) request. getAttribute ("usersList"); ArrayList usersList = (ArrayList) pageResultSet. getData (); for (int I = 0; I <usersList. size (); I ++) {Contact co = (Contact) usersList. get (I); %> <% = Co. getId () %> "> <% = Co. getUsername () %> <% = Co. getMobile () %> <% = Co. getMail () %> <% = Co. getPhone () %> <% = Co. getMem () %> <% = Co. getLastcontact () %> <% }%> <% = PageResultSet. getToolBar ("login. do") %> For more useful JSP paging code articles, please follow the PHP Chinese network!

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.