1.Pager.java
2.PagerHelper.java
3. UserDAO:
4.UserAction:
5.UserInfo.jsp:
package com.fzw.util;
public class Pager {
private int totalRows;
private int totalPages;
private int pageSize=5;
private int currentPage;
private int startRow; //定义为前一页的最后一行为开始行
public Pager( int totalRows){ //初始化
this.totalRows = totalRows;
this.totalPages = totalRows/pageSize;
int mod = totalRows%pageSize;
if(mod>0){ //如果不能整除,totalPages加1
this.totalPages++;
}
this.currentPage = 1;
startRow = 0; //起始行为第0行
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage( int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize( int pageSize) {
this.pageSize = pageSize;
}
public int getStartRow() {
return startRow;
}
public void setStartRow( int startRow) {
this.startRow = startRow;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages( int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows( int totalRows) {
this.totalRows = totalRows;
}
public void first(){ //获得第一页
currentPage = 1;
startRow = 0;
}
public void previous(){ //获得前一页
if(currentPage == 1){ //如果当前页是第一页,那么什么也不做
return;
}
currentPage--;
startRow = (currentPage-1)*pageSize;
}
public void next(){ //获得下一页
if(currentPage currentPage++;
}
startRow = (currentPage-1)*pageSize;
}
public void last(){ //获得最后一页
currentPage = totalPages;
startRow = (currentPage-1)*pageSize;
}
public void refresh( int currentPage){ //获得指定页面
this.currentPage = currentPage;
if(currentPage > totalPages){
last();
}
}
}
public class Pager {
private int totalRows;
private int totalPages;
private int pageSize=5;
private int currentPage;
private int startRow; //定义为前一页的最后一行为开始行
public Pager( int totalRows){ //初始化
this.totalRows = totalRows;
this.totalPages = totalRows/pageSize;
int mod = totalRows%pageSize;
if(mod>0){ //如果不能整除,totalPages加1
this.totalPages++;
}
this.currentPage = 1;
startRow = 0; //起始行为第0行
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage( int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize( int pageSize) {
this.pageSize = pageSize;
}
public int getStartRow() {
return startRow;
}
public void setStartRow( int startRow) {
this.startRow = startRow;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages( int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows( int totalRows) {
this.totalRows = totalRows;
}
public void first(){ //获得第一页
currentPage = 1;
startRow = 0;
}
public void previous(){ //获得前一页
if(currentPage == 1){ //如果当前页是第一页,那么什么也不做
return;
}
currentPage--;
startRow = (currentPage-1)*pageSize;
}
public void next(){ //获得下一页
if(currentPage
}
startRow = (currentPage-1)*pageSize;
}
public void last(){ //获得最后一页
currentPage = totalPages;
startRow = (currentPage-1)*pageSize;
}
public void refresh( int currentPage){ //获得指定页面
this.currentPage = currentPage;
if(currentPage > totalPages){
last();
}
}
}
2.PagerHelper.java
package com.fzw.util;
import javax.servlet.http.HttpServletRequest;
public class PagerHelper {
public static Pager getPager(HttpServletRequest request, int totalRows) {
Pager pager = new Pager(totalRows);
String currentPage = request.getParameter( "currentPage");
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}
String pagerMethod = request.getParameter( "pagerMethod");
if (pagerMethod != null) {
if (pagerMethod.equals( "first")) {
pager.first();
} else if (pagerMethod.equals( "previous")) {
pager.previous();
} else if (pagerMethod.equals( "next")) {
pager.next();
} else if (pagerMethod.equals( "last")) {
pager.last();
}
}
return pager;
}
}
import javax.servlet.http.HttpServletRequest;
public class PagerHelper {
public static Pager getPager(HttpServletRequest request, int totalRows) {
Pager pager = new Pager(totalRows);
String currentPage = request.getParameter( "currentPage");
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}
String pagerMethod = request.getParameter( "pagerMethod");
if (pagerMethod != null) {
if (pagerMethod.equals( "first")) {
pager.first();
} else if (pagerMethod.equals( "previous")) {
pager.previous();
} else if (pagerMethod.equals( "next")) {
pager.next();
} else if (pagerMethod.equals( "last")) {
pager.last();
}
}
return pager;
}
}
3. UserDAO:
public Collection findWithPage(
final Pager pager){
return this.getHibernateTemplate().executeFind( new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException{
Query query = session.createQuery( "from User");
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
return query.list();
}
});
}
return this.getHibernateTemplate().executeFind( new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException{
Query query = session.createQuery( "from User");
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
return query.list();
}
});
}
4.UserAction:
public ActionForward show(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Collection clUsers = null;
List list = userService.findAll();
int totalRows = list.size();
Pager pager = PagerHelper.getPager(request,totalRows);
clUsers = userService.findWithPage(pager);
request.setAttribute( "clUsers", clUsers);
request.setAttribute( "pager", pager);
return mapping.findForward( "success");
}
HttpServletRequest request, HttpServletResponse response) {
Collection clUsers = null;
List list = userService.findAll();
int totalRows = list.size();
Pager pager = PagerHelper.getPager(request,totalRows);
clUsers = userService.findWithPage(pager);
request.setAttribute( "clUsers", clUsers);
request.setAttribute( "pager", pager);
return mapping.findForward( "success");
}
5.UserInfo.jsp:
----------
"pager" name=
"pager">
"/user.do?methodCode=show&pagerMethod=first">首页
"/user.do?methodCode=show&pagerMethod=previous"
paramId= "currentPage" paramName= "pager" paramProperty= "currentPage">上一页
"/user.do?methodCode=show&pagerMethod=next"
paramId= "currentPage" paramName= "pager" paramProperty= "currentPage">下一页
"/user.do?methodCode=show&pagerMethod=last"
paramId= "currentPage" paramName= "pager" paramProperty= "currentPage">尾页
第"pager" property=
"currentPage" />页
共"pager" property=
"totalPages" />页
注:其中的paramId,paramName,paramProperty没起作用!!!
paramId= "currentPage" paramName= "pager" paramProperty= "currentPage">上一页
paramId= "currentPage" paramName= "pager" paramProperty= "currentPage">下一页
paramId= "currentPage" paramName= "pager" paramProperty= "currentPage">尾页
第
共
注:其中的paramId,paramName,paramProperty没起作用!!!