在平时的java开发中,我们常常会为数据分页而苦恼,常常是把上一个项目的代码复制粘贴,这样并不符合我们企业级开发中的做到程序的可复用的一个要求,本例中的分页组件由一个java类和js文件组成,可以做到排序和分页的效果,在客户端的调用也相当方便,如果你还苦恼于分页的麻烦,就看看下面的代码吧。
本例中我们以Oracle中建立数据库时scott测试用户的Emp为例进行演示。
先看看效果吧:
第一页:
第二页:
第三页:
1.Emp类: Emp.java
Java代码
package com.css.model;
import java.util.Date;
public class Emp implements java.io.Serializable {
private Long empno;
private String ename;
private String job;
private Long mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Long deptno;
public Emp() {
}
// Property accessors
}
package com.css.model;
import java.util.Date;
public class Emp implements java.io.Serializable {
private Long empno;
private String ename;
private String job;
private Long mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Long deptno;
public Emp() {
}
// Property accessors
}
2.Emp.hbm.xml省略不写,用MyEclipses可以自动生成。
3.下面是最重要的Page类。
Java代码
/*
* Created on 2005-6-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.css.util;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
/**
* @author Administrator TODO To change the template for this generated type
* comment go to Window - Preferences - Java - Code Style - Code
* Templates
*/
public class Page implements Serializable {
private static Log log = LogFactory.getLog(Page.class);
private int pageSize = 5;
private int currentPage = 1;
private int totalRows;
private int totalPages;
private List results;
private long startTime;
private long endTime;
private int orderFlag;
private String orderString;
public void finalize() throws Throwable {
gc();
super.finalize();
}
public void gc() {
if (this.results != null) {
this.results.clear();
this.results = null;
}
this.orderString = null;
}
public void initPage(int totalRows, int pageSize) {
this.totalRows = totalRows;
this.pageSize = pageSize;
initPageInfo();
}
public void initPage(Query query, Query queryRows) {
if(this.pageSize==0){
this.pageSize= 5;
}
getQueryRows(queryRows);
this.startTime = System.currentTimeMillis();
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
}
public void initPage(Query query) {
this.startTime = System.currentTimeMillis();
this.pageSize = -1;
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
}
public void initPage(Criteria criteria) {
try {
this.startTime = System.currentTimeMillis();
if (this.pageSize != -1) {
getQueryRows(criteria);
criteria.setProjection(null);
}
this.results = getQueryResult(criteria);
this.endTime = System.currentTimeMillis();
} catch (Exception e) {
log.error("Page::initPage(Criteria):" + e.getMessage());
}
}
private List getQueryResult(Query query) {
List listResult;
if (this.pageSize == -1) {
listResult = query.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else {
if (currentPage < 1)
currentPage = 1;
listResult = query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
}
return listResult;
}
private List getQueryResult(Criteria criteria) {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
if (this.orderFlag == 0)
criteria.addOrder(Order.asc(this.orderString));
else
criteria.addOrder(Order.desc(this.orderString));
}
List listResult;
if (this.pageSize == -1) {
listResult = criteria.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else
listResult = criteria.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
return listResult;
}
private void getQueryRows(Criteria criteria) {
this.totalRows = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
}
private void getQueryRows(Query query) {
try {
this.startTime = System.currentTimeMillis();
this.totalRows = ((Integer) query.list().get(0)).intValue();
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
} catch (Exception ex) {
log.error(ex.getMessage());
this.totalRows = 0;
}
initPageInfo();
}
public void initPageInfo() {
this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize;
this.totalPages = this.totalPages < 1 ? 1 : this.totalPages;
this.currentPage = this.currentPage > this.totalPages ? this.totalPages : this.currentPage;
}
public boolean isNextPage() {
return currentPage < totalPages;
}
public boolean isPreviousPage() {
return currentPage > 1;
}
public long getDiffTime() {
return this.endTime - this.startTime;
}
/**
* @return Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageSize.
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
* The pageSize to set.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return Returns the results.
*/
public List getResults() {
return results;
}
/**
* @param results
* The results to set.
*/
public void setResults(List results) {
this.results = results;
}
/**
* @return Returns the totalRows.
*/
public int getTotalRows() {
return totalRows;
}
/**
* @param totalRows
* The totalRows to set.
*/
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
/**
* @return Returns the totalPages.
*/
public int getTotalPages() {
return totalPages;
}
/**
* @return Returns the orderFlag.
*/
public int getOrderFlag() {
return orderFlag;
}
/**
* @param orderFlag
* The orderFlag to set.
*/
public void setOrderFlag(int orderFlag) {
this.orderFlag = orderFlag;
}
/**
* @return Returns the orderString.
*/
public String getOrderString() {
return orderString;
}
/**
* @param orderString
* The orderString to set.
*/
public void setOrderString(String orderString) {
this.orderString = orderString;
}
public String getOrderByString() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = " order by " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getOrderByString2() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = ", " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getPageSplit() {
StringBuffer sb = new StringBuffer();
if (this.pageSize == -1 || this.totalPages == 1)
sb.append("共1页 记录总数: <b>" + totalRows + " </b>条");
else {
sb.append("<a href='javascript:skipToPage(1)'>首页</a> ");
if (this.isPreviousPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage - 1) + ")\">上一页</a> ");
if (this.isNextPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage + 1) + ")\">下一页</a> ");
sb.append("<a href=\"javascript:skipToPage(" + this.totalPages + ")\">尾页</a> ");
sb.append("第<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.currentPage\" id=\"page.currentPage\" value=\"" + currentPage + "\"/>");
sb.append("页 <input class=buttonJump type=button onclick=commonJump() name=goto value=Go> 共" + totalPages
+ "页 每页");
sb.append("<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.pageSize\" id=\"page.pageSize\" value=\"" + pageSize + "\"/>");
sb.append("条 记录总数:<b>" + totalRows + "</b>条");
}
return sb.toString();
}
public String getPageInfo(int type, String fun) {
return getPageInfo(type, fun, "");
}
public String getPageInfo(int type, String fun, String objName) {
if (fun == null || fun.length() == 0)
fun = "jump";
StringBuffer sb = new StringBuffer();
sb.append("<div id=pageTop>");
if (this.totalPages == 1) {
sb.append("共1页 总计<b>" + this.totalRows + "</b>条");
} else {
if (this.currentPage > 1)
sb.append("<a href=javascript:" + fun + "ToPage(1)>首页</a> <a href=javascript:" + fun + "ToPage("
+ (this.currentPage - 1) + ")>上一页</a> ");
if (this.currentPage < this.totalPages)
sb.append("<a href=javascript:" + fun + "ToPage(" + (this.currentPage + 1)
+ ")>下一页</a> <a href=javascript:" + fun + "ToPage(" + this.totalPages + ")>尾页</a> ");
sb.append("第<input size=3 maxlength=6 name=cp" + type + " id=" + objName + "cp" + type + " value="
+ this.currentPage + ">页 <input class=buttonJump type=button onclick=" + fun + "(" + type + ",'"
+ objName + "') name=goto" + type + " value=Go>");
sb.append(" 共" + this.totalPages + "页 每页" + this.pageSize + "条 总计<b>" + this.totalRows + "</b>条");
}
sb.append("</div>");
return sb.toString();
}
public String getPageTop(String fun) {
return getPageInfo(1, fun);
}
public String getPageBottom(String fun) {
return getPageInfo(2, fun);
}
public String getObjPageTop(String fun, String objName) {
return getPageInfo(1, fun, objName);
}
public String getObjPageBottom(String fun, String objName) {
return getPageInfo(2, fun, objName);
}
}
/*
* Created on 2005-6-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.css.util;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
/**
* @author Administrator TODO To change the template for this generated type
* comment go to Window - Preferences - Java - Code Style - Code
* Templates
*/
public class Page implements Serializable {
private static Log log = LogFactory.getLog(Page.class);
private int pageSize = 5;
private int currentPage = 1;
private int totalRows;
private int totalPages;
private List results;
private long startTime;
private long endTime;
private int orderFlag;
private String orderString;
public void finalize() throws Throwable {
gc();
super.finalize();
}
public void gc() {
if (this.results != null) {
this.results.clear();
this.results = null;
}
this.orderString = null;
}
public void initPage(int totalRows, int pageSize) {
this.totalRows = totalRows;
this.pageSize = pageSize;
initPageInfo();
}
public void initPage(Query query, Query queryRows) {
if(this.pageSize==0){
this.pageSize= 5;
}
getQueryRows(queryRows);
this.startTime = System.currentTimeMillis();
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
}
public void initPage(Query query) {
this.startTime = System.currentTimeMillis();
this.pageSize = -1;
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
}
public void initPage(Criteria criteria) {
try {
this.startTime = System.currentTimeMillis();
if (this.pageSize != -1) {
getQueryRows(criteria);
criteria.setProjection(null);
}
this.results = getQueryResult(criteria);
this.endTime = System.currentTimeMillis();
} catch (Exception e) {
log.error("Page::initPage(Criteria):" + e.getMessage());
}
}
private List getQueryResult(Query query) {
List listResult;
if (this.pageSize == -1) {
listResult = query.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else {
if (currentPage < 1)
currentPage = 1;
listResult = query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
}
return listResult;
}
private List getQueryResult(Criteria criteria) {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
if (this.orderFlag == 0)
criteria.addOrder(Order.asc(this.orderString));
else
criteria.addOrder(Order.desc(this.orderString));
}
List listResult;
if (this.pageSize == -1) {
listResult = criteria.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else
listResult = criteria.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
return listResult;
}
private void getQueryRows(Criteria criteria) {
this.totalRows = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
}
private void getQueryRows(Query query) {
try {
this.startTime = System.currentTimeMillis();
this.totalRows = ((Integer) query.list().get(0)).intValue();
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
} catch (Exception ex) {
log.error(ex.getMessage());
this.totalRows = 0;
}
initPageInfo();
}
public void initPageInfo() {
this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize;
this.totalPages = this.totalPages < 1 ? 1 : this.totalPages;
this.currentPage = this.currentPage > this.totalPages ? this.totalPages : this.currentPage;
}
public boolean isNextPage() {
return currentPage < totalPages;
}
public boolean isPreviousPage() {
return currentPage > 1;
}
public long getDiffTime() {
return this.endTime - this.startTime;
}
/**
* @return Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageSize.
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
* The pageSize to set.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return Returns the results.
*/
public List getResults() {
return results;
}
/**
* @param results
* The results to set.
*/
public void setResults(List results) {
this.results = results;
}
/**
* @return Returns the totalRows.
*/
public int getTotalRows() {
return totalRows;
}
/**
* @param totalRows
* The totalRows to set.
*/
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
/**
* @return Returns the totalPages.
*/
public int getTotalPages() {
return totalPages;
}
/**
* @return Returns the orderFlag.
*/
public int getOrderFlag() {
return orderFlag;
}
/**
* @param orderFlag
* The orderFlag to set.
*/
public void setOrderFlag(int orderFlag) {
this.orderFlag = orderFlag;
}
/**
* @return Returns the orderString.
*/
public String getOrderString() {
return orderString;
}
/**
* @param orderString
* The orderString to set.
*/
public void setOrderString(String orderString) {
this.orderString = orderString;
}
public String getOrderByString() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = " order by " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getOrderByString2() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = ", " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getPageSplit() {
StringBuffer sb = new StringBuffer();
if (this.pageSize == -1 || this.totalPages == 1)
sb.append("共1页 记录总数: <b>" + totalRows + " </b>条");
else {
sb.append("<a href='javascript:skipToPage(1)'>首页</a> ");
if (this.isPreviousPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage - 1) + ")\">上一页</a> ");
if (this.isNextPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage + 1) + ")\">下一页</a> ");
sb.append("<a href=\"javascript:skipToPage(" + this.totalPages + ")\">尾页</a> ");
sb.append("第<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.currentPage\" id=\"page.currentPage\" value=\"" + currentPage + "\"/>");
sb.append("页 <input class=buttonJump type=button onclick=commonJump() name=goto value=Go> 共" + totalPages
+ "页 每页");
sb.append("<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.pageSize\" id=\"page.pageSize\" value=\"" + pageSize + "\"/>");
sb.append("条 记录总数:<b>" + totalRows + "</b>条");
}
return sb.toString();
}
public String getPageInfo(int type, String fun) {
return getPageInfo(type, fun, "");
}
public String getPageInfo(int type, String fun, String objName) {
if (fun == null || fun.length() == 0)
fun = "jump";
StringBuffer sb = new StringBuffer();
sb.append("<div id=pageTop>");
if (this.totalPages == 1) {
sb.append("共1页 总计<b>" + this.totalRows + "</b>条");
} else {
if (this.currentPage > 1)
sb.append("<a href=javascript:" + fun + "ToPage(1)>首页</a> <a href=javascript:" + fun + "ToPage("
+ (this.currentPage - 1) + ")>上一页</a> ");
if (this.currentPage < this.totalPages)
sb.append("<a href=javascript:" + fun + "ToPage(" + (this.currentPage + 1)
+ ")>下一页</a> <a href=javascript:" + fun + "ToPage(" + this.totalPages + ")>尾页</a> ");
sb.append("第<input size=3 maxlength=6 name=cp" + type + " id=" + objName + "cp" + type + " value="
+ this.currentPage + ">页 <input class=buttonJump type=button onclick=" + fun + "(" + type + ",'"
+ objName + "') name=goto" + type + " value=Go>");
sb.append(" 共" + this.totalPages + "页 每页" + this.pageSize + "条 总计<b>" + this.totalRows + "</b>条");
}
sb.append("</div>");
return sb.toString();
}
public String getPageTop(String fun) {
return getPageInfo(1, fun);
}
public String getPageBottom(String fun) {
return getPageInfo(2, fun);
}
public String getObjPageTop(String fun, String objName) {
return getPageInfo(1, fun, objName);
}
public String getObjPageBottom(String fun, String objName) {
return getPageInfo(2, fun, objName);
}
}
4.EmpDAO.java类
Java代码
package com.css.dao;
import org.hibernate.Query;
import org.hibernate.Session;
import com.css.util.Page;
public class EmpDAO {
public boolean searchUser(Page page, String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
Query queryRows = session.createQuery("select count(*) " + hql);
page.initPage(query, queryRows);
HibernateSessionFactory.closeSession();
return true;
}
}
package com.css.dao;
import org.hibernate.Query;
import org.hibernate.Session;
import com.css.util.Page;
public class EmpDAO {
public boolean searchUser(Page page, String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
Query queryRows = session.createQuery("select count(*) " + hql);
page.initPage(query, queryRows);
HibernateSessionFactory.closeSession();
return true;
}
}
5.EmpSearchAction.java类
Java代码
package com.css.action;
import com.css.dao.EmpDAO;
import com.css.util.Page;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
public class EmpSearchAction extends ActionSupport implements ModelDriven {
private Page page;
public EmpSearchAction() {
page = new Page();
page.setCurrentPage(1);
}
@Override
public String execute() throws Exception {
String hql = "from Emp";
hql += page.getOrderByString();
EmpDAO empDAO = new EmpDAO();
empDAO.searchUser(page, hql);
return SUCCESS;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Object getModel() {
return page;
}
}
package com.css.action;
import com.css.dao.EmpDAO;
import com.css.util.Page;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
public class EmpSearchAction extends ActionSupport implements ModelDriven {
private Page page;
public EmpSearchAction() {
page = new Page();
page.setCurrentPage(1);
}
@Override
public String execute() throws Exception {
String hql = "from Emp";
hql += page.getOrderByString();
EmpDAO empDAO = new EmpDAO();
empDAO.searchUser(page, hql);
return SUCCESS;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Object getModel() {
return page;
}
}
6.xwork.xml配置文件
Xml代码
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="empSearch"
class="com.css.action.EmpSearchAction">
<result name="success" type="dispatcher">
<param name="location">/diremp.jsp</param>
</result>
<interceptor-ref name="params" />
</action>
</package>
</xwork>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="empSearch"
class="com.css.action.EmpSearchAction">
<result name="success" type="dispatcher">
<param name="location">/diremp.jsp</param>
</result>
<interceptor-ref name="params" />
</action>
</package>
</xwork>
7.page.js文件
Java代码
var ie;
if (document.all) ie=1;
else ie=0;
function isnull(str){if(str==null||str==""||str=="undefine")return true;
return false;
}
function killErrors() {
return true;
}
window.onerror = killErrors;
function commonJump(){
skipToPage(document.getElementById('page.currentPage').value);
}
//-----------------分页-----------------------------------------------
function skipToPage(page)
{
document.getElementById('page.currentPage').value=page;
document.form1.submit();
}
function SetOrder(str)
{
var orderFlag=0;
document.getElementById('page.orderString').value=str;
if(!isnull(document.getElementById('page.orderFlag').value))
orderFlag=document.getElementById('page.orderFlag').value;
document.getElementById('page.orderFlag').value=1 - orderFlag;
document.form1.submit();
}
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
String.prototype.allTrim=function(){
return this.replace(/(\s*)/g, "");
}
function querySubmit(){
$('keyword').value=$('keyword').value.allTrim();
$('cid').value=$('cid').value.allTrim();
document.form1.submit();
}
var ie;
if (document.all) ie=1;
else ie=0;
function isnull(str){if(str==null||str==""||str=="undefine")return true;
return false;
}
function killErrors() {
return true;
}
window.onerror = killErrors;
function commonJump(){
skipToPage(document.getElementById('page.currentPage').value);
}
//-----------------分页-----------------------------------------------
function skipToPage(page)
{
document.getElementById('page.currentPage').value=page;
document.form1.submit();
}
function SetOrder(str)
{
var orderFlag=0;
document.getElementById('page.orderString').value=str;
if(!isnull(document.getElementById('page.orderFlag').value))
orderFlag=document.getElementById('page.orderFlag').value;
document.getElementById('page.orderFlag').value=1 - orderFlag;
document.form1.submit();
}
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
String.prototype.allTrim=function(){
return this.replace(/(\s*)/g, "");
}
function querySubmit(){
$('keyword').value=$('keyword').value.allTrim();
$('cid').value=$('cid').value.allTrim();
document.form1.submit();
}
8.diremp.jsp
Java代码
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="ww" uri="webwork"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Page类测试</title>
<script type="text/javascript" src="page.js"></script>
</head>
<body>
<form name="form1" id="form1" method="post" action="empSearch.action">
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
<table width="100%" border="1" cellspacing="1" cellpadding="0"
class="datatb">
<tr>
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('job')">职位</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('hiredate')">雇佣时间</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('sal')">基本工资</a>
</td>
</tr>
<ww:iterator value="page.results" id="data">
<tr
onmouseover="currentcolor=this.style.backgroundColor;this.style.backgroundColor='#fff6cb';this.style.cursor='hand';"
onmouseout="this.style.backgroundColor=currentcolor">
<td title="<ww:property value="empno"/>">
<ww:property value="empno" />
</td>
<td title="<ww:property value="ename" />">
<ww:property value="ename" />
</td>
<td title="<ww:property value="job" />">
<ww:property value="job" />
</td>
<td title="<ww:property value="hiredate" />">
<ww:property value="hiredate" />
</td>
<td title="<ww:property value="sal" />">
<ww:property value="sal" />
</td>
</tr>
</ww:iterator>
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="ww" uri="webwork"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Page类测试</title>
<script type="text/javascript" src="page.js"></script>
</head>
<body>
<form name="form1" id="form1" method="post" action="empSearch.action">
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
<table width="100%" border="1" cellspacing="1" cellpadding="0"
class="datatb">
<tr>
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('job')">职位</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('hiredate')">雇佣时间</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('sal')">基本工资</a>
</td>
</tr>
<ww:iterator value="page.results" id="data">
<tr
onmouseover="currentcolor=this.style.backgroundColor;this.style.backgroundColor='#fff6cb';this.style.cursor='hand';"
onmouseout="this.style.backgroundColor=currentcolor">
<td title="<ww:property value="empno"/>">
<ww:property value="empno" />
</td>
<td title="<ww:property value="ename" />">
<ww:property value="ename" />
</td>
<td title="<ww:property value="job" />">
<ww:property value="job" />
</td>
<td title="<ww:property value="hiredate" />">
<ww:property value="hiredate" />
</td>
<td title="<ww:property value="sal" />">
<ww:property value="sal" />
</td>
</tr>
</ww:iterator>
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr>
</table>
</form>
</body>
</html>
注意点:
1) 在前台jsp页面中只需加上如下代码即可:
Html代码
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr>
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr> 2) 在标题中设置排序字段:
Html代码
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td> 3) <form>表单中还要传递两个隐藏参数:
Html代码
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
本例中我们以Oracle中建立数据库时scott测试用户的Emp为例进行演示。
先看看效果吧:
第一页:
第二页:
第三页:
1.Emp类: Emp.java
Java代码
package com.css.model;
import java.util.Date;
public class Emp implements java.io.Serializable {
private Long empno;
private String ename;
private String job;
private Long mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Long deptno;
public Emp() {
}
// Property accessors
}
package com.css.model;
import java.util.Date;
public class Emp implements java.io.Serializable {
private Long empno;
private String ename;
private String job;
private Long mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Long deptno;
public Emp() {
}
// Property accessors
}
2.Emp.hbm.xml省略不写,用MyEclipses可以自动生成。
3.下面是最重要的Page类。
Java代码
/*
* Created on 2005-6-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.css.util;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
/**
* @author Administrator TODO To change the template for this generated type
* comment go to Window - Preferences - Java - Code Style - Code
* Templates
*/
public class Page implements Serializable {
private static Log log = LogFactory.getLog(Page.class);
private int pageSize = 5;
private int currentPage = 1;
private int totalRows;
private int totalPages;
private List results;
private long startTime;
private long endTime;
private int orderFlag;
private String orderString;
public void finalize() throws Throwable {
gc();
super.finalize();
}
public void gc() {
if (this.results != null) {
this.results.clear();
this.results = null;
}
this.orderString = null;
}
public void initPage(int totalRows, int pageSize) {
this.totalRows = totalRows;
this.pageSize = pageSize;
initPageInfo();
}
public void initPage(Query query, Query queryRows) {
if(this.pageSize==0){
this.pageSize= 5;
}
getQueryRows(queryRows);
this.startTime = System.currentTimeMillis();
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
}
public void initPage(Query query) {
this.startTime = System.currentTimeMillis();
this.pageSize = -1;
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
}
public void initPage(Criteria criteria) {
try {
this.startTime = System.currentTimeMillis();
if (this.pageSize != -1) {
getQueryRows(criteria);
criteria.setProjection(null);
}
this.results = getQueryResult(criteria);
this.endTime = System.currentTimeMillis();
} catch (Exception e) {
log.error("Page::initPage(Criteria):" + e.getMessage());
}
}
private List getQueryResult(Query query) {
List listResult;
if (this.pageSize == -1) {
listResult = query.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else {
if (currentPage < 1)
currentPage = 1;
listResult = query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
}
return listResult;
}
private List getQueryResult(Criteria criteria) {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
if (this.orderFlag == 0)
criteria.addOrder(Order.asc(this.orderString));
else
criteria.addOrder(Order.desc(this.orderString));
}
List listResult;
if (this.pageSize == -1) {
listResult = criteria.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else
listResult = criteria.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
return listResult;
}
private void getQueryRows(Criteria criteria) {
this.totalRows = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
}
private void getQueryRows(Query query) {
try {
this.startTime = System.currentTimeMillis();
this.totalRows = ((Integer) query.list().get(0)).intValue();
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
} catch (Exception ex) {
log.error(ex.getMessage());
this.totalRows = 0;
}
initPageInfo();
}
public void initPageInfo() {
this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize;
this.totalPages = this.totalPages < 1 ? 1 : this.totalPages;
this.currentPage = this.currentPage > this.totalPages ? this.totalPages : this.currentPage;
}
public boolean isNextPage() {
return currentPage < totalPages;
}
public boolean isPreviousPage() {
return currentPage > 1;
}
public long getDiffTime() {
return this.endTime - this.startTime;
}
/**
* @return Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageSize.
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
* The pageSize to set.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return Returns the results.
*/
public List getResults() {
return results;
}
/**
* @param results
* The results to set.
*/
public void setResults(List results) {
this.results = results;
}
/**
* @return Returns the totalRows.
*/
public int getTotalRows() {
return totalRows;
}
/**
* @param totalRows
* The totalRows to set.
*/
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
/**
* @return Returns the totalPages.
*/
public int getTotalPages() {
return totalPages;
}
/**
* @return Returns the orderFlag.
*/
public int getOrderFlag() {
return orderFlag;
}
/**
* @param orderFlag
* The orderFlag to set.
*/
public void setOrderFlag(int orderFlag) {
this.orderFlag = orderFlag;
}
/**
* @return Returns the orderString.
*/
public String getOrderString() {
return orderString;
}
/**
* @param orderString
* The orderString to set.
*/
public void setOrderString(String orderString) {
this.orderString = orderString;
}
public String getOrderByString() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = " order by " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getOrderByString2() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = ", " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getPageSplit() {
StringBuffer sb = new StringBuffer();
if (this.pageSize == -1 || this.totalPages == 1)
sb.append("共1页 记录总数: <b>" + totalRows + " </b>条");
else {
sb.append("<a href='javascript:skipToPage(1)'>首页</a> ");
if (this.isPreviousPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage - 1) + ")\">上一页</a> ");
if (this.isNextPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage + 1) + ")\">下一页</a> ");
sb.append("<a href=\"javascript:skipToPage(" + this.totalPages + ")\">尾页</a> ");
sb.append("第<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.currentPage\" id=\"page.currentPage\" value=\"" + currentPage + "\"/>");
sb.append("页 <input class=buttonJump type=button onclick=commonJump() name=goto value=Go> 共" + totalPages
+ "页 每页");
sb.append("<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.pageSize\" id=\"page.pageSize\" value=\"" + pageSize + "\"/>");
sb.append("条 记录总数:<b>" + totalRows + "</b>条");
}
return sb.toString();
}
public String getPageInfo(int type, String fun) {
return getPageInfo(type, fun, "");
}
public String getPageInfo(int type, String fun, String objName) {
if (fun == null || fun.length() == 0)
fun = "jump";
StringBuffer sb = new StringBuffer();
sb.append("<div id=pageTop>");
if (this.totalPages == 1) {
sb.append("共1页 总计<b>" + this.totalRows + "</b>条");
} else {
if (this.currentPage > 1)
sb.append("<a href=javascript:" + fun + "ToPage(1)>首页</a> <a href=javascript:" + fun + "ToPage("
+ (this.currentPage - 1) + ")>上一页</a> ");
if (this.currentPage < this.totalPages)
sb.append("<a href=javascript:" + fun + "ToPage(" + (this.currentPage + 1)
+ ")>下一页</a> <a href=javascript:" + fun + "ToPage(" + this.totalPages + ")>尾页</a> ");
sb.append("第<input size=3 maxlength=6 name=cp" + type + " id=" + objName + "cp" + type + " value="
+ this.currentPage + ">页 <input class=buttonJump type=button onclick=" + fun + "(" + type + ",'"
+ objName + "') name=goto" + type + " value=Go>");
sb.append(" 共" + this.totalPages + "页 每页" + this.pageSize + "条 总计<b>" + this.totalRows + "</b>条");
}
sb.append("</div>");
return sb.toString();
}
public String getPageTop(String fun) {
return getPageInfo(1, fun);
}
public String getPageBottom(String fun) {
return getPageInfo(2, fun);
}
public String getObjPageTop(String fun, String objName) {
return getPageInfo(1, fun, objName);
}
public String getObjPageBottom(String fun, String objName) {
return getPageInfo(2, fun, objName);
}
}
/*
* Created on 2005-6-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.css.util;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
/**
* @author Administrator TODO To change the template for this generated type
* comment go to Window - Preferences - Java - Code Style - Code
* Templates
*/
public class Page implements Serializable {
private static Log log = LogFactory.getLog(Page.class);
private int pageSize = 5;
private int currentPage = 1;
private int totalRows;
private int totalPages;
private List results;
private long startTime;
private long endTime;
private int orderFlag;
private String orderString;
public void finalize() throws Throwable {
gc();
super.finalize();
}
public void gc() {
if (this.results != null) {
this.results.clear();
this.results = null;
}
this.orderString = null;
}
public void initPage(int totalRows, int pageSize) {
this.totalRows = totalRows;
this.pageSize = pageSize;
initPageInfo();
}
public void initPage(Query query, Query queryRows) {
if(this.pageSize==0){
this.pageSize= 5;
}
getQueryRows(queryRows);
this.startTime = System.currentTimeMillis();
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
}
public void initPage(Query query) {
this.startTime = System.currentTimeMillis();
this.pageSize = -1;
this.results = getQueryResult(query);
this.endTime = System.currentTimeMillis();
}
public void initPage(Criteria criteria) {
try {
this.startTime = System.currentTimeMillis();
if (this.pageSize != -1) {
getQueryRows(criteria);
criteria.setProjection(null);
}
this.results = getQueryResult(criteria);
this.endTime = System.currentTimeMillis();
} catch (Exception e) {
log.error("Page::initPage(Criteria):" + e.getMessage());
}
}
private List getQueryResult(Query query) {
List listResult;
if (this.pageSize == -1) {
listResult = query.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else {
if (currentPage < 1)
currentPage = 1;
listResult = query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
}
return listResult;
}
private List getQueryResult(Criteria criteria) {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
if (this.orderFlag == 0)
criteria.addOrder(Order.asc(this.orderString));
else
criteria.addOrder(Order.desc(this.orderString));
}
List listResult;
if (this.pageSize == -1) {
listResult = criteria.list();
this.totalRows = listResult.size();
this.totalPages = 1;
this.currentPage = 1;
} else
listResult = criteria.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
return listResult;
}
private void getQueryRows(Criteria criteria) {
this.totalRows = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
}
private void getQueryRows(Query query) {
try {
this.startTime = System.currentTimeMillis();
this.totalRows = ((Integer) query.list().get(0)).intValue();
this.endTime = System.currentTimeMillis();
System.out.println(this.endTime - this.startTime);
} catch (Exception ex) {
log.error(ex.getMessage());
this.totalRows = 0;
}
initPageInfo();
}
public void initPageInfo() {
this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize;
this.totalPages = this.totalPages < 1 ? 1 : this.totalPages;
this.currentPage = this.currentPage > this.totalPages ? this.totalPages : this.currentPage;
}
public boolean isNextPage() {
return currentPage < totalPages;
}
public boolean isPreviousPage() {
return currentPage > 1;
}
public long getDiffTime() {
return this.endTime - this.startTime;
}
/**
* @return Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageSize.
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
* The pageSize to set.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return Returns the results.
*/
public List getResults() {
return results;
}
/**
* @param results
* The results to set.
*/
public void setResults(List results) {
this.results = results;
}
/**
* @return Returns the totalRows.
*/
public int getTotalRows() {
return totalRows;
}
/**
* @param totalRows
* The totalRows to set.
*/
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
/**
* @return Returns the totalPages.
*/
public int getTotalPages() {
return totalPages;
}
/**
* @return Returns the orderFlag.
*/
public int getOrderFlag() {
return orderFlag;
}
/**
* @param orderFlag
* The orderFlag to set.
*/
public void setOrderFlag(int orderFlag) {
this.orderFlag = orderFlag;
}
/**
* @return Returns the orderString.
*/
public String getOrderString() {
return orderString;
}
/**
* @param orderString
* The orderString to set.
*/
public void setOrderString(String orderString) {
this.orderString = orderString;
}
public String getOrderByString() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = " order by " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getOrderByString2() {
String sRet = "";
if (this.orderString != null && !this.orderString.equals("")) {
sRet = ", " + this.orderString + (this.orderFlag == 1 ? " " : " desc ");
}
return sRet;
}
public String getPageSplit() {
StringBuffer sb = new StringBuffer();
if (this.pageSize == -1 || this.totalPages == 1)
sb.append("共1页 记录总数: <b>" + totalRows + " </b>条");
else {
sb.append("<a href='javascript:skipToPage(1)'>首页</a> ");
if (this.isPreviousPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage - 1) + ")\">上一页</a> ");
if (this.isNextPage())
sb.append("<a href=\"javascript:skipToPage(" + (this.currentPage + 1) + ")\">下一页</a> ");
sb.append("<a href=\"javascript:skipToPage(" + this.totalPages + ")\">尾页</a> ");
sb.append("第<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.currentPage\" id=\"page.currentPage\" value=\"" + currentPage + "\"/>");
sb.append("页 <input class=buttonJump type=button onclick=commonJump() name=goto value=Go> 共" + totalPages
+ "页 每页");
sb.append("<input size=\"3\" maxlength=\"6\" onkeyup=\"value=value.replace(/[^\\d]/g,'')\"");
sb
.append(" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"");
sb.append(" name=\"page.pageSize\" id=\"page.pageSize\" value=\"" + pageSize + "\"/>");
sb.append("条 记录总数:<b>" + totalRows + "</b>条");
}
return sb.toString();
}
public String getPageInfo(int type, String fun) {
return getPageInfo(type, fun, "");
}
public String getPageInfo(int type, String fun, String objName) {
if (fun == null || fun.length() == 0)
fun = "jump";
StringBuffer sb = new StringBuffer();
sb.append("<div id=pageTop>");
if (this.totalPages == 1) {
sb.append("共1页 总计<b>" + this.totalRows + "</b>条");
} else {
if (this.currentPage > 1)
sb.append("<a href=javascript:" + fun + "ToPage(1)>首页</a> <a href=javascript:" + fun + "ToPage("
+ (this.currentPage - 1) + ")>上一页</a> ");
if (this.currentPage < this.totalPages)
sb.append("<a href=javascript:" + fun + "ToPage(" + (this.currentPage + 1)
+ ")>下一页</a> <a href=javascript:" + fun + "ToPage(" + this.totalPages + ")>尾页</a> ");
sb.append("第<input size=3 maxlength=6 name=cp" + type + " id=" + objName + "cp" + type + " value="
+ this.currentPage + ">页 <input class=buttonJump type=button onclick=" + fun + "(" + type + ",'"
+ objName + "') name=goto" + type + " value=Go>");
sb.append(" 共" + this.totalPages + "页 每页" + this.pageSize + "条 总计<b>" + this.totalRows + "</b>条");
}
sb.append("</div>");
return sb.toString();
}
public String getPageTop(String fun) {
return getPageInfo(1, fun);
}
public String getPageBottom(String fun) {
return getPageInfo(2, fun);
}
public String getObjPageTop(String fun, String objName) {
return getPageInfo(1, fun, objName);
}
public String getObjPageBottom(String fun, String objName) {
return getPageInfo(2, fun, objName);
}
}
4.EmpDAO.java类
Java代码
package com.css.dao;
import org.hibernate.Query;
import org.hibernate.Session;
import com.css.util.Page;
public class EmpDAO {
public boolean searchUser(Page page, String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
Query queryRows = session.createQuery("select count(*) " + hql);
page.initPage(query, queryRows);
HibernateSessionFactory.closeSession();
return true;
}
}
package com.css.dao;
import org.hibernate.Query;
import org.hibernate.Session;
import com.css.util.Page;
public class EmpDAO {
public boolean searchUser(Page page, String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
Query queryRows = session.createQuery("select count(*) " + hql);
page.initPage(query, queryRows);
HibernateSessionFactory.closeSession();
return true;
}
}
5.EmpSearchAction.java类
Java代码
package com.css.action;
import com.css.dao.EmpDAO;
import com.css.util.Page;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
public class EmpSearchAction extends ActionSupport implements ModelDriven {
private Page page;
public EmpSearchAction() {
page = new Page();
page.setCurrentPage(1);
}
@Override
public String execute() throws Exception {
String hql = "from Emp";
hql += page.getOrderByString();
EmpDAO empDAO = new EmpDAO();
empDAO.searchUser(page, hql);
return SUCCESS;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Object getModel() {
return page;
}
}
package com.css.action;
import com.css.dao.EmpDAO;
import com.css.util.Page;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
public class EmpSearchAction extends ActionSupport implements ModelDriven {
private Page page;
public EmpSearchAction() {
page = new Page();
page.setCurrentPage(1);
}
@Override
public String execute() throws Exception {
String hql = "from Emp";
hql += page.getOrderByString();
EmpDAO empDAO = new EmpDAO();
empDAO.searchUser(page, hql);
return SUCCESS;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Object getModel() {
return page;
}
}
6.xwork.xml配置文件
Xml代码
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="empSearch"
class="com.css.action.EmpSearchAction">
<result name="success" type="dispatcher">
<param name="location">/diremp.jsp</param>
</result>
<interceptor-ref name="params" />
</action>
</package>
</xwork>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="empSearch"
class="com.css.action.EmpSearchAction">
<result name="success" type="dispatcher">
<param name="location">/diremp.jsp</param>
</result>
<interceptor-ref name="params" />
</action>
</package>
</xwork>
7.page.js文件
Java代码
var ie;
if (document.all) ie=1;
else ie=0;
function isnull(str){if(str==null||str==""||str=="undefine")return true;
return false;
}
function killErrors() {
return true;
}
window.onerror = killErrors;
function commonJump(){
skipToPage(document.getElementById('page.currentPage').value);
}
//-----------------分页-----------------------------------------------
function skipToPage(page)
{
document.getElementById('page.currentPage').value=page;
document.form1.submit();
}
function SetOrder(str)
{
var orderFlag=0;
document.getElementById('page.orderString').value=str;
if(!isnull(document.getElementById('page.orderFlag').value))
orderFlag=document.getElementById('page.orderFlag').value;
document.getElementById('page.orderFlag').value=1 - orderFlag;
document.form1.submit();
}
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
String.prototype.allTrim=function(){
return this.replace(/(\s*)/g, "");
}
function querySubmit(){
$('keyword').value=$('keyword').value.allTrim();
$('cid').value=$('cid').value.allTrim();
document.form1.submit();
}
var ie;
if (document.all) ie=1;
else ie=0;
function isnull(str){if(str==null||str==""||str=="undefine")return true;
return false;
}
function killErrors() {
return true;
}
window.onerror = killErrors;
function commonJump(){
skipToPage(document.getElementById('page.currentPage').value);
}
//-----------------分页-----------------------------------------------
function skipToPage(page)
{
document.getElementById('page.currentPage').value=page;
document.form1.submit();
}
function SetOrder(str)
{
var orderFlag=0;
document.getElementById('page.orderString').value=str;
if(!isnull(document.getElementById('page.orderFlag').value))
orderFlag=document.getElementById('page.orderFlag').value;
document.getElementById('page.orderFlag').value=1 - orderFlag;
document.form1.submit();
}
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
String.prototype.allTrim=function(){
return this.replace(/(\s*)/g, "");
}
function querySubmit(){
$('keyword').value=$('keyword').value.allTrim();
$('cid').value=$('cid').value.allTrim();
document.form1.submit();
}
8.diremp.jsp
Java代码
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="ww" uri="webwork"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Page类测试</title>
<script type="text/javascript" src="page.js"></script>
</head>
<body>
<form name="form1" id="form1" method="post" action="empSearch.action">
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
<table width="100%" border="1" cellspacing="1" cellpadding="0"
class="datatb">
<tr>
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('job')">职位</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('hiredate')">雇佣时间</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('sal')">基本工资</a>
</td>
</tr>
<ww:iterator value="page.results" id="data">
<tr
onmouseover="currentcolor=this.style.backgroundColor;this.style.backgroundColor='#fff6cb';this.style.cursor='hand';"
onmouseout="this.style.backgroundColor=currentcolor">
<td title="<ww:property value="empno"/>">
<ww:property value="empno" />
</td>
<td title="<ww:property value="ename" />">
<ww:property value="ename" />
</td>
<td title="<ww:property value="job" />">
<ww:property value="job" />
</td>
<td title="<ww:property value="hiredate" />">
<ww:property value="hiredate" />
</td>
<td title="<ww:property value="sal" />">
<ww:property value="sal" />
</td>
</tr>
</ww:iterator>
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="ww" uri="webwork"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Page类测试</title>
<script type="text/javascript" src="page.js"></script>
</head>
<body>
<form name="form1" id="form1" method="post" action="empSearch.action">
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
<table width="100%" border="1" cellspacing="1" cellpadding="0"
class="datatb">
<tr>
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('job')">职位</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('hiredate')">雇佣时间</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('sal')">基本工资</a>
</td>
</tr>
<ww:iterator value="page.results" id="data">
<tr
onmouseover="currentcolor=this.style.backgroundColor;this.style.backgroundColor='#fff6cb';this.style.cursor='hand';"
onmouseout="this.style.backgroundColor=currentcolor">
<td title="<ww:property value="empno"/>">
<ww:property value="empno" />
</td>
<td title="<ww:property value="ename" />">
<ww:property value="ename" />
</td>
<td title="<ww:property value="job" />">
<ww:property value="job" />
</td>
<td title="<ww:property value="hiredate" />">
<ww:property value="hiredate" />
</td>
<td title="<ww:property value="sal" />">
<ww:property value="sal" />
</td>
</tr>
</ww:iterator>
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr>
</table>
</form>
</body>
</html>
注意点:
1) 在前台jsp页面中只需加上如下代码即可:
Html代码
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr>
<tr>
<td align="center" colspan="10">
<ww:property value="page.pageSplit" />
</td>
</tr> 2) 在标题中设置排序字段:
Html代码
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('empno')">编号</a>
</td>
<td class="datath" width="15%">
<a href="javascript:SetOrder('ename')">姓名</a>
</td> 3) <form>表单中还要传递两个隐藏参数:
Html代码
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
<input type="hidden" name="page.orderFlag" id="page.orderFlag"
value="<ww:property value="page.orderFlag" />">
<input type="hidden" name="page.orderString" id="page.orderString"
value="<ww:property value="page.orderString"/>">
- hibernate01.rar (25.8 KB)
- 下载次数: 1
相关推荐
NULL 博文链接:https://yanxinfeng.iteye.com/blog/524496
http://peterwei.javaeye.com/blog/968815 Ubuntu10下JDK1.6安装 http://peterwei.javaeye.com/blog/968758 Ubuntu10下Tomcat7安装 http://peterwei.javaeye.com/blog/968774 Ubuntu10下Eclipse3.6安装 ...
Learning XNA 4.0 中文版全书下载 ...我的博客: 博客园: http://www.cnblogs.com/peixiaoxing: 主要发布游戏开发方面的文章。 CSDN: http://blog.csdn.net/xxing22657 主要发布.NET平台相关文章。 javaeye: ...
第一阶段(2周左右每天...参考网文:http://jackweijie.javaeye.com/blog/191452 参考书:Flex 入门教程http://www.5uflash.com/Flex-AIR/Flexziliao/17.html flex入门介绍:http://download.csdn.net/source/452847
Flex + Hessian 学习笔记(一) http://wangcheng.javaeye.com/blog/141382 Flex + Hessian 学习笔记(二) http://wangcheng.javaeye.com/blog/141539 Flex + Hessian 学习笔记(三) ...
http://comsci.javaeye.com/ JWFD工作流设计器(带引擎算法)的源代码包, 实在不好意思,这个包的资源文件缺失了,导致编译时报错,经过反馈, 修改了这个问题,在资源中新增加下载,本下载停止使用,非常抱歉 请...
本附件下载自 http://www.javaeye.com/topic/626010
Android 中文API 合集(102 篇)
在Struts中应用标签的数据库分页示例(dbpage.jsp),打开方法: ...打开方法: http://localhost:8080/pagertaglib/test1.jsp<br><br><br><br>制作示例参考了: ...
更多内容请访问:http://ecsun.javaeye.com 作者:海鹏 联系:myecsun@hotmail.com/Q:82676683/ 1.技术体系 Ext2.1,Struts1.3,Hibernate3.2,Spring2.5,Acegi 1.06,MySQL 2.功能概述: Blog,RSS订阅,网址导航 3.特色 ...
keygen 2.X注册机 ...至少NOD32就会报且阻止不能运行,试试吧 看着官方的安装包不大,就一同打包了 ...有博客(绝不是我的,呵呵)对这软件有些评价,闲的人就点点看http://pure.javaeye.com/blog/432945?page=2
-- 配置集群的时候,要用到,在这篇文章:“apache+tomcat集群、负载均衡及session复制”里的第三条(http://jiajun.javaeye.com/admin/blogs/278586) --> <distributable/> <!-- context-param 元素用来设定...
IK Expression是一个开源的...http://linliangyi2007.javaeye.com/blog/337069 GoogleCode开源项目 :http://code.google.com/p/ik-expression/ GoogleCode SVN下载:http://ik-expression.googlecode.com/svn/trunk/
已解决FCKeditor在Weblogic下发布Error Loading错误的例子,采用了网上比较流行的"通过虚拟地址解决fckeditor错误的加载/fckeditor/fckstyles.xml”,具体方法可参照:...
转javaeye:http://da-en.javaeye.com/blog/712415 我们知道要让TextView解析和显示Html代码。可以使用 Spanned text = Html.fromHtml(source); tv.setText(text); 来实现,这个用起来简单方便。 但是,怎样让...