1. 分页处理的JavaBean文件
public class PagerPublic {
private int totalRows; //总行数
private int pageSize = 20; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行
String url = null;
public PagerPublic (int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 0;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 1;
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}
public void setUrl(String url)
{
this.url = url;
}
/**
*
* @return
*/
public boolean hasFirstPage(){
if (getCurrentPage()>1){
return true;
}
else return false;
}
public boolean hasLastPage(){
if ((getCurrentPage()<getTotalPages())&&(getTotalPages()!=1)){
return true;
}
else return false;
}
/**
* 是否有下一页
* @return 是否有下一页
*/
public boolean hasNextPage() {
if (getTotalRows()>pageSize) {
// currentPage++;
// startRow = (currentPage - 1) * pageSize;
return (this.getCurrentPage()<this.getTotalPages());
}
else return false;
}
/**
* 是否有上一页
* @return 是否有上一页
*/
public boolean hasPreviousPage() {
return (this.getCurrentPage()>1);
}
public String getHTML(){
StringBuffer html = new StringBuffer();
if (hasFirstPage()){
html.append("<a href=/"")
.append(url)
.append("&pageNo=1/">首页")
.append("</a>");
}else
{
html.append(" ")
.append("首页");
}
if (hasPreviousPage()){
html.append(" <a href=/"")
.append(url)
.append("&pageNo=")
.append(getCurrentPage()-1)
.append("/">上页")
.append("</a>");
} else{
html.append(" ")
.append("上页");
}
if (hasNextPage()){
html.append(" <a href=/"")
.append(url)
.append("&pageNo=")
.append(getCurrentPage()+1)
.append("/">下页")
.append("</a>");
} else{
html.append(" ")
.append("下页");
}
if (hasLastPage()){
html.append(" <a href=/"")
.append(url)
.append("&pageNo=")
.append(getTotalPages())
.append("/">末页")
.append("</a> ");
} else{
html.append(" ")
.append("末页");
}
html.append("【第").append(getCurrentPage()).append("页】");
html.append("【共").append(getTotalPages()).append("页】");
html.append("【共").append(getTotalRows()).append("条】");
return html.toString();
}
}
--------------------------------------------------------------------
阿里妈妈,诚信、透明的网上广告交易平台
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
---------------------------------------------------------------------
为sohu等各类博客量身定做的侧边栏天气预报智能Flash插件
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
2. dao代码:
public List findByExample(String hql) {
try {
List results = getHibernateTemplate().find(hql);
log.debug("find by example successful," + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByPage(int startRow, int rows, String hql) {
Session session=getSession();
try {
query = session.createQuery(hql);
query.setFirstResult(startRow);
query.setMaxResults(rows);
List results =query.list();
session.close();
log.debug("find by page successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
session.close();
log.error("find by page failed", re);
throw re;
}
}
3. 调用分页的代码,对应的Action文件
部分代码如下:
String PageNo = request.getParameter("pageNo");//页码
String sql = "from JavaBean";
List l = (ArrayList) this.JavaBeanDao.findByExample(sql);
int total = l.size();
int pageNo = Integer.parseInt(request, "pageNo", 1);
PagerPublic pg = new PagerPublic(total);
pg.setUrl("/list.do");
pg.setCurrentPage(pageNo);
List rsPage = (List) this.JavaBeanDao.findByPage((pg.getPageSize())
* (pageNo - 1), pg.getPageSize(), sql);
ArrayList list = new ArrayList();
if (!rsPage.isEmpty()) {
Iterator it = rsPage.iterator();
for (; it.hasNext();) {
JavaBean p = (JavaBean) it.next();
JavaBeanForm c = new JavaBeanForm();
c.setId(p.getId());
list.add(c);
}
}
request.setAttribute("showPage", pg.getHTML());
request.setAttribute("rs", list);
return mapping.findForward("index");
程序代码
public class PagerPublic {
private int totalRows; //总行数
private int pageSize = 20; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行
String url = null;
public PagerPublic (int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 0;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 1;
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}
public void setUrl(String url)
{
this.url = url;
}
/**
*
* @return
*/
public boolean hasFirstPage(){
if (getCurrentPage()>1){
return true;
}
else return false;
}
public boolean hasLastPage(){
if ((getCurrentPage()<getTotalPages())&&(getTotalPages()!=1)){
return true;
}
else return false;
}
/**
* 是否有下一页
* @return 是否有下一页
*/
public boolean hasNextPage() {
if (getTotalRows()>pageSize) {
// currentPage++;
// startRow = (currentPage - 1) * pageSize;
return (this.getCurrentPage()<this.getTotalPages());
}
else return false;
}
/**
* 是否有上一页
* @return 是否有上一页
*/
public boolean hasPreviousPage() {
return (this.getCurrentPage()>1);
}
public String getHTML(){
StringBuffer html = new StringBuffer();
if (hasFirstPage()){
html.append("<a href=/"")
.append(url)
.append("&pageNo=1/">首页")
.append("</a>");
}else
{
html.append(" ")
.append("首页");
}
if (hasPreviousPage()){
html.append(" <a href=/"")
.append(url)
.append("&pageNo=")
.append(getCurrentPage()-1)
.append("/">上页")
.append("</a>");
} else{
html.append(" ")
.append("上页");
}
if (hasNextPage()){
html.append(" <a href=/"")
.append(url)
.append("&pageNo=")
.append(getCurrentPage()+1)
.append("/">下页")
.append("</a>");
} else{
html.append(" ")
.append("下页");
}
if (hasLastPage()){
html.append(" <a href=/"")
.append(url)
.append("&pageNo=")
.append(getTotalPages())
.append("/">末页")
.append("</a> ");
} else{
html.append(" ")
.append("末页");
}
html.append("【第").append(getCurrentPage()).append("页】");
html.append("【共").append(getTotalPages()).append("页】");
html.append("【共").append(getTotalRows()).append("条】");
return html.toString();
}
}
--------------------------------------------------------------------
阿里妈妈,诚信、透明的网上广告交易平台
HTML代码
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
---------------------------------------------------------------------
为sohu等各类博客量身定做的侧边栏天气预报智能Flash插件
HTML代码
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
2. dao代码:
程序代码
public List findByExample(String hql) {
try {
List results = getHibernateTemplate().find(hql);
log.debug("find by example successful," + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByPage(int startRow, int rows, String hql) {
Session session=getSession();
try {
query = session.createQuery(hql);
query.setFirstResult(startRow);
query.setMaxResults(rows);
List results =query.list();
session.close();
log.debug("find by page successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
session.close();
log.error("find by page failed", re);
throw re;
}
}
3. 调用分页的代码,对应的Action文件
部分代码如下:
程序代码
String PageNo = request.getParameter("pageNo");//页码
String sql = "from JavaBean";
List l = (ArrayList) this.JavaBeanDao.findByExample(sql);
int total = l.size();
int pageNo = Integer.parseInt(request, "pageNo", 1);
PagerPublic pg = new PagerPublic(total);
pg.setUrl("/list.do");
pg.setCurrentPage(pageNo);
List rsPage = (List) this.JavaBeanDao.findByPage((pg.getPageSize())
* (pageNo - 1), pg.getPageSize(), sql);
ArrayList list = new ArrayList();
if (!rsPage.isEmpty()) {
Iterator it = rsPage.iterator();
for (; it.hasNext();) {
JavaBean p = (JavaBean) it.next();
JavaBeanForm c = new JavaBeanForm();
c.setId(p.getId());
list.add(c);
}
}
request.setAttribute("showPage", pg.getHTML());
request.setAttribute("rs", list);
return mapping.findForward("index");