以下内容省去了有关service在spring配置文件中代码以及service Interface类,只写了service的实现类
1、用于分页查询的Action在struts配置文件中的代码片段:
<action name="adminAction" class="ssh.web.action.comm.AdminAction" method="query">
<result name="success">/main/webapp/query/queryResult.jsp</result>
</action>
2、Action的代码:
package ssh.web.action.comm;
//管理员action实现类
import java.util.List;
import ssh.comm.AdminService;
import ssh.comm.Page;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings({ "unchecked", "serial" })
public class AdminAction extends ActionSupport {
//查看管理员
private int pno; //查看的表单页数
private AdminService adminService;
private Page myPage;
private List mylist;
private String stuName; //传递的用户名
public String query() {
System.out.println("stuName= "+ stuName +" pno= "+pno);
if (pno == 0 ) {
pno = 1;
}
adminService.init(pno);
myPage = adminService.getPage();
// mylist = adminService.getPage().getList();
return SUCCESS;
}
public int getPno() {
return pno;
}
public void setPno(int pno) {
this.pno = pno;
}
public AdminService getAdminService() {
return adminService;
}
public void setAdminService(AdminService adminService) {
this.adminService = adminService;
}
public Page getMyPage() {
return myPage;
}
public void setMyPage(Page myPage) {
this.myPage = myPage;
}
public List getMylist() {
return mylist;
}
public void setMylist(List mylist) {
this.mylist = mylist;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
3、adminService的实现类
package ssh.comm.impl;
import ssh.comm.AdminService;
import ssh.comm.Page;
import ssh.comm.PageDao;
public class AdminServiceImpl implements AdminService{
private PageDao pageDao;
public Page getPage() {
return pageDao.getPage();
}
public void init(int pno) {
String tableName="Student";
pageDao.init(pno, tableName);
}
public PageDao getPageDao() {
return pageDao;
}
public void setPageDao(PageDao pageDao) {
this.pageDao = pageDao;
}
}
4、pageDao的实现类
package ssh.comm.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import ssh.comm.Page;
import ssh.comm.PageDao;
public class PageDaoImpl extends HibernateDaoSupport implements PageDao{
private String hql;
public Page page;
public int start;
public void init(int start,String tableName){
page = new Page();
this.hql = "from "+tableName;
this.start = start;
setRowCount(); //设置page的总数量
setTotalPage(); //设置page的总页数
setCurrentPage(); //设置page的当前页
setPrePage(); //设置page的上一页
setNextPage(); //设置page的下一页
setPreOrNextBoolean(); //判断page是否有上一页或下一页
}
@SuppressWarnings("unchecked")
public Page getPage(){
List list = (List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());
return query.list();
}
});
page.setList(list);
return page;
}
/**获得数据的总数量*/
@SuppressWarnings("unchecked")
public int getRowCount(){
List list = (List)getHibernateTemplate().find(hql);
if(list.isEmpty()){
return 0;
}
return list.size();
}
/**
* 判断是否有下一页
* */
public void setPreOrNextBoolean() {
if (page.getCurrentPage() <= 1) {
page.setHasPreviousPage(false);
} else {
page.setHasPreviousPage(true);
}
if (page.getCurrentPage() >= page.getTotalPage()) {
page.setHasNextPage(false);
} else {
page.setHasNextPage(true);
}
}
/**
* 设置当前查询页
* */
public void setCurrentPage() {
if (start < 1) {
page.setCurrentPage(1);
}
else if (start > page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
}
else {
page.setCurrentPage(start);
}
}
/**
* 设置上一页的页数
* */
public void setPrePage() {
page.setPrePage(page.getCurrentPage() - 1);
}
/**
* 设置下一页的页数
* */
public void setNextPage() {
page.setNextPage(page.getCurrentPage() + 1);
}
/**
* 计算总页数
* */
public void setTotalPage() {
int rowCount = getRowCount();
int pageSize = page.getPageSize();
if (rowCount > pageSize) {
if (rowCount % pageSize == 0) {
page.setTotalPage(rowCount / pageSize);
} else {
page.setTotalPage(1 + (rowCount / pageSize));
}
} else {
page.setTotalPage(1);
}
}
public void setRowCount() {
page.setRowCount(getRowCount());
}
/**
* 设置查询数据的起始位置
* */
public int getStartIndex() {
int startIndex = 0;
if (start < 0) {
startIndex = 0;
} else {
if (start > page.getTotalPage()) {
startIndex = page.getPageSize() * (page.getTotalPage() - 1);
} else {
startIndex = page.getPageSize() * (start - 1);
}
}
return startIndex;
}
}
5、Page类对象
package ssh.comm;
import java.io.Serializable;
import java.util.List;
public class Page implements Serializable{
private static final long serialVersionUID = -7129370623545540245L;
private int pageSize; //每页的数量
private int totalPage; //总页数
private int rowCount; //总数量
private int currentPage; //当前页数
private int prePage; //上一页
private int nextPage; //下一页
private boolean hasNextPage; //是否有下一页
private boolean hasPreviousPage; //是否有下一页
@SuppressWarnings("unchecked")
private List list; //返回的数据
public Page() {
this.pageSize=6;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
@SuppressWarnings("unchecked")
public List getList() {
return list;
}
@SuppressWarnings("unchecked")
public void setList(List list) {
this.list = list;
}
}
6、展示数据的jsp文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<html>
<head>
<script src="/s2sh/main/webapp/res/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="/s2sh/main/webapp/css/editTable.css" />
<script type="text/javascript" src="/s2sh/main/webapp/query/editTable.js"></script>
<title>查询结果页面</title>
</head>
<body>
查询结果<br>
<table >
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>出生年月</th>
<th>家庭住址</th>
</tr>
</thead>
<tbody>
<c:forEach items="${myPage.list}" var="student" >
<tr>
<td align="center">${student.stuId}</td>
<td align="center">${student.stuName}</td>
<td align="center">${student.stuSex}</td>
<td align="center">${student.stuBir }</td>
<td align="center">${student.stuAdd }</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="6">
<div class="message" > </div>
<div class="pagebar list_page">
共<span class="rowCount"> ${myPage.rowCount} </span>条
<span class="currentPage">${myPage.currentPage} </span>/<span class="totalPage"> ${myPage.totalPage} </span>
<a class="firstPage" href="/s2sh/student/adminAction.action?pno=1">首页</a>
<a class="prePage" href="/s2sh/student/adminAction.action?pno=${myPage.prePage}" >上一页</a>
<a class="nextPage" href="/s2sh/student/adminAction.action?pno=${myPage.nextPage}" >下一页</a>
<a class="lastPage" href="/s2sh/student/adminAction.action?pno=${myPage.totalPage}">尾页</a>
跳转到第 <input type="text" class="currentPage inputW20" /> 页 <input type="hidden" class="pageSize"></input>
</div>
</td>
</tr>
</tfoot>
</table>
</body>
</html>
1、用于分页查询的Action在struts配置文件中的代码片段:
<action name="adminAction" class="ssh.web.action.comm.AdminAction" method="query">
<result name="success">/main/webapp/query/queryResult.jsp</result>
</action>
2、Action的代码:
package ssh.web.action.comm;
//管理员action实现类
import java.util.List;
import ssh.comm.AdminService;
import ssh.comm.Page;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings({ "unchecked", "serial" })
public class AdminAction extends ActionSupport {
//查看管理员
private int pno; //查看的表单页数
private AdminService adminService;
private Page myPage;
private List mylist;
private String stuName; //传递的用户名
public String query() {
System.out.println("stuName= "+ stuName +" pno= "+pno);
if (pno == 0 ) {
pno = 1;
}
adminService.init(pno);
myPage = adminService.getPage();
// mylist = adminService.getPage().getList();
return SUCCESS;
}
public int getPno() {
return pno;
}
public void setPno(int pno) {
this.pno = pno;
}
public AdminService getAdminService() {
return adminService;
}
public void setAdminService(AdminService adminService) {
this.adminService = adminService;
}
public Page getMyPage() {
return myPage;
}
public void setMyPage(Page myPage) {
this.myPage = myPage;
}
public List getMylist() {
return mylist;
}
public void setMylist(List mylist) {
this.mylist = mylist;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
3、adminService的实现类
package ssh.comm.impl;
import ssh.comm.AdminService;
import ssh.comm.Page;
import ssh.comm.PageDao;
public class AdminServiceImpl implements AdminService{
private PageDao pageDao;
public Page getPage() {
return pageDao.getPage();
}
public void init(int pno) {
String tableName="Student";
pageDao.init(pno, tableName);
}
public PageDao getPageDao() {
return pageDao;
}
public void setPageDao(PageDao pageDao) {
this.pageDao = pageDao;
}
}
4、pageDao的实现类
package ssh.comm.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import ssh.comm.Page;
import ssh.comm.PageDao;
public class PageDaoImpl extends HibernateDaoSupport implements PageDao{
private String hql;
public Page page;
public int start;
public void init(int start,String tableName){
page = new Page();
this.hql = "from "+tableName;
this.start = start;
setRowCount(); //设置page的总数量
setTotalPage(); //设置page的总页数
setCurrentPage(); //设置page的当前页
setPrePage(); //设置page的上一页
setNextPage(); //设置page的下一页
setPreOrNextBoolean(); //判断page是否有上一页或下一页
}
@SuppressWarnings("unchecked")
public Page getPage(){
List list = (List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(getStartIndex());
query.setMaxResults(page.getPageSize());
return query.list();
}
});
page.setList(list);
return page;
}
/**获得数据的总数量*/
@SuppressWarnings("unchecked")
public int getRowCount(){
List list = (List)getHibernateTemplate().find(hql);
if(list.isEmpty()){
return 0;
}
return list.size();
}
/**
* 判断是否有下一页
* */
public void setPreOrNextBoolean() {
if (page.getCurrentPage() <= 1) {
page.setHasPreviousPage(false);
} else {
page.setHasPreviousPage(true);
}
if (page.getCurrentPage() >= page.getTotalPage()) {
page.setHasNextPage(false);
} else {
page.setHasNextPage(true);
}
}
/**
* 设置当前查询页
* */
public void setCurrentPage() {
if (start < 1) {
page.setCurrentPage(1);
}
else if (start > page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
}
else {
page.setCurrentPage(start);
}
}
/**
* 设置上一页的页数
* */
public void setPrePage() {
page.setPrePage(page.getCurrentPage() - 1);
}
/**
* 设置下一页的页数
* */
public void setNextPage() {
page.setNextPage(page.getCurrentPage() + 1);
}
/**
* 计算总页数
* */
public void setTotalPage() {
int rowCount = getRowCount();
int pageSize = page.getPageSize();
if (rowCount > pageSize) {
if (rowCount % pageSize == 0) {
page.setTotalPage(rowCount / pageSize);
} else {
page.setTotalPage(1 + (rowCount / pageSize));
}
} else {
page.setTotalPage(1);
}
}
public void setRowCount() {
page.setRowCount(getRowCount());
}
/**
* 设置查询数据的起始位置
* */
public int getStartIndex() {
int startIndex = 0;
if (start < 0) {
startIndex = 0;
} else {
if (start > page.getTotalPage()) {
startIndex = page.getPageSize() * (page.getTotalPage() - 1);
} else {
startIndex = page.getPageSize() * (start - 1);
}
}
return startIndex;
}
}
5、Page类对象
package ssh.comm;
import java.io.Serializable;
import java.util.List;
public class Page implements Serializable{
private static final long serialVersionUID = -7129370623545540245L;
private int pageSize; //每页的数量
private int totalPage; //总页数
private int rowCount; //总数量
private int currentPage; //当前页数
private int prePage; //上一页
private int nextPage; //下一页
private boolean hasNextPage; //是否有下一页
private boolean hasPreviousPage; //是否有下一页
@SuppressWarnings("unchecked")
private List list; //返回的数据
public Page() {
this.pageSize=6;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
@SuppressWarnings("unchecked")
public List getList() {
return list;
}
@SuppressWarnings("unchecked")
public void setList(List list) {
this.list = list;
}
}
6、展示数据的jsp文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<html>
<head>
<script src="/s2sh/main/webapp/res/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="/s2sh/main/webapp/css/editTable.css" />
<script type="text/javascript" src="/s2sh/main/webapp/query/editTable.js"></script>
<title>查询结果页面</title>
</head>
<body>
查询结果<br>
<table >
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>出生年月</th>
<th>家庭住址</th>
</tr>
</thead>
<tbody>
<c:forEach items="${myPage.list}" var="student" >
<tr>
<td align="center">${student.stuId}</td>
<td align="center">${student.stuName}</td>
<td align="center">${student.stuSex}</td>
<td align="center">${student.stuBir }</td>
<td align="center">${student.stuAdd }</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="6">
<div class="message" > </div>
<div class="pagebar list_page">
共<span class="rowCount"> ${myPage.rowCount} </span>条
<span class="currentPage">${myPage.currentPage} </span>/<span class="totalPage"> ${myPage.totalPage} </span>
<a class="firstPage" href="/s2sh/student/adminAction.action?pno=1">首页</a>
<a class="prePage" href="/s2sh/student/adminAction.action?pno=${myPage.prePage}" >上一页</a>
<a class="nextPage" href="/s2sh/student/adminAction.action?pno=${myPage.nextPage}" >下一页</a>
<a class="lastPage" href="/s2sh/student/adminAction.action?pno=${myPage.totalPage}">尾页</a>
跳转到第 <input type="text" class="currentPage inputW20" /> 页 <input type="hidden" class="pageSize"></input>
</div>
</td>
</tr>
</tfoot>
</table>
</body>
</html>