// Refresh the current page information
public void refresh() {
if (totalPages <= 1) {
hasPrevious = false;
hasNext = false;
} else if (currentPage == 1) {
hasPrevious = false;
hasNext = true;
} else if (currentPage == totalPages) {
hasPrevious = true;
hasNext = false;
} else {
hasPrevious = true;
hasNext = true;
}
}
}
Action 1:
public ActionForward queryWithPage(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletresponse) {
Collection clInfos = null;//Collection of records used to output to the page
int totalRows;//Record the total number of rows
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO();
//Get the total number of rows in the current table
try {
totalRows = ("select count(*) from VehicleProperty");
} catch (Exception ex) {
(());
return ();
}
//Get the pager object used to output to the page through the PagerHelper class
Pager pager=(httpServletRequest,totalRows);
//Retrieve the pageSize line record starting from startRow
try {
clInfos = ((), ());
} catch (Exception ex) {
(());
return ();
}
//Save the output record set and pager object into the request object
("CLINFOS", clInfos);
("PAGER", pager);
return ();
}
The query statement select count(*) from VehicleProperty can also be replaced with any condition you need (select count(*) from VehicleProperty where ..)
Action 2:
DisplayAllAction Display Data Page Controller
package ;
import .*;
import ;
import .*;
/*
* @(#) 2005-5-2
*
* Copyright (c) 2005, Jeffrey Xu
*/
import .*;
import .*;
import .*;
/**
* Display page controller
*/
public class DisplayAllAction extends Action {
private HibernateDAO hibernateDAO = new HibernateDAO();
private Pager pager = new Pager();
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = ();
List messageList = null;
HQuery hquery = new HQuery();
int totalRows = 0;
int startRow = 0;
try {
totalRows = ("select count(*) from Message");
// Initialize page information
(totalRows, Constants.RECORD_PER_PAGE);
} catch (Exception ex) {
();
}
("From Message order by m_sendDate desc");
String viewPage = (String) ("viewPage");
String action = (String) ("action");
// Jump to the corresponding page
if (viewPage != null && !("")) {
try {
((viewPage));
} catch (NumberFormatException e) {
();
}
}
if (action != null) {
// Control the forward and backward page according to the passed parameters
if (("previous")) {
();
} else if (("next")) {
();
} else if (("first")) {
();
} else if (("last")) {
();
}
}
try {
(());
messageList = (hquery);
} catch (Exception ex) {
();
}
("list", messageList);
("pager", pager);
return ("display");
}
}
PagerHelp class and DAO in Action
package ;
import .*;
public class PagerHelper {
public static Pager getPager(HttpServletRequest httpServletRequest,
int totalRows) {
//Define the pager object for transmission to the page
Pager pager = new Pager(totalRows);
//Get the current page number from the Request object
String currentPage = ("currentPage");
//If the current page number is empty, it means that the page is first queryed
//If it is not empty, refresh the pager object and enter the current page number and other information
if (currentPage != null) {
((currentPage));
}
//Get the current execution method, home page, previous page, next page, last page.
String pagerMethod = ("pageMethod");
if (pagerMethod != null) {
if (("first")) {
();
} else if (("previous")) {
();
} else if (("next")) {
();
} else if (("last")) {
();
}
}
return pager;
}
}
DAO class
package ;
import .*;
import ;
import .*;
import .*;
import .*;
public class VehiclePropertyDAO {
public Collection findWithPage(int pageSize, int startRow) throws
HibernateException {
Collection vehicleList = null;
Transaction tx = null;
try {
Session session = ();
tx = ();
Query q = ("from VehicleProperty vp");
(startRow);
(pageSize);
vehicleList = ();
();
} catch (HibernateException he) {
if (tx != null) {
();
}
throw he;
} finally {
();
}
return vehicleList;
}
public int getRows(String query) throws
HibernateException {
int totalRows = 0;
Transaction tx = null;
try {
Session session = ();
tx = ();
totalRows = ((Integer) (query).next()).
intValue();
();
} catch (HibernateException he) {
if (tx != null) {
();
}
throw he;
} finally {
();
}
return totalRows;
}
}
Previous page123Next pageRead the full text