六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 116|回复: 0

比较好用的分页组件(转http://sxpujs.javaeye.com/blog/437447)

[复制链接]

升级  4%

16

主题

16

主题

16

主题

秀才

Rank: 2

积分
56
 楼主| 发表于 2013-2-7 22:24:17 | 显示全部楼层 |阅读模式
在平时的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 (" + 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 (" + 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   
                          
                        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

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"/>">
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表