SSH——基于datagrid实现分页查询

1. 修改页面中datagridURL地址,访问action

    // 取派员信息表格
        $('#grid').datagrid( {
            iconCls : 'icon-forward',
            fit : true,
            border : false,
            rownumbers : true,//显示行号
            striped : true,
            pageList: [3,5,10],
            pagination : true,
            toolbar : toolbar,//工具栏
            url : "${pageContext.request.contextPath}/staffAction_pageQuery.action",   //这里修改url
            idField : 'id',
            columns : columns,
            onDblClickRow : doDblClickRow//指定数据表格的双击行事件
        });

项目路径src\com\zang\bos\web\action下staffAction.java中的StaffAction类

/**
 * 取派员管理
 * @author zhaoqx
 *
 */
@Controller
@Scope("prototype")
public class StaffAction extends BaseAction<Staff>{
    //注入Service
    @Autowired
    private IStaffService staffService;
    
    private int page;//页码
    private int rows;//每页显示的记录数
    public void setRows(int rows) {
        this.rows = rows;
    }
    
    public void setPage(int page) {
        this.page = page;
    }

2. 创建PageBean类,封装分页信息

/**
 * 封装分页信息
 * @author zhaoqx
 *
 */
public class PageBean {
    private int currentPage;//当前页码
    private int pageSize;//每页显示记录数
    private DetachedCriteria detachedCriteria;//离线条件查询对象,包装查询条件
    private int total;//总记录数
    private List rows;//当前页需要展示的数据集合

public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public DetachedCriteria getDetachedCriteria() { return detachedCriteria; } public void setDetachedCriteria(DetachedCriteria detachedCriteria) { this.detachedCriteria = detachedCriteria; } public List getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } }

3. StaffAction中提供pageQuery方法,提供两个setPagesetRows方法,接收页面提交参数

    private int page;//页码
    private int rows;//每页显示的记录数
    public void setRows(int rows) {
        this.rows = rows;
    }
    
    public void setPage(int page) {
        this.page = page;
    }
/** * 分页查询方法 * @throws IOException */ public String pageQuery() throws IOException{ PageBean pageBean = new PageBean(); pageBean.setCurrentPage(page); pageBean.setPageSize(rows); DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Staff.class); pageBean.setDetachedCriteria(detachedCriteria); staffService.pageQuery(pageBean); //将PageBean对象转为json返回 JSONObject jsonObject = JSONObject.fromObject(pageBean); String json = jsonObject.toString(); ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8"); ServletActionContext.getResponse().getWriter().print(json); return NONE; }

4. 在src\com\zang\bos\dao路径下BaseDao中提供通用分页查询方法

      /**
     * 通用分页查询方法
     */
    public void pageQuery(PageBean pageBean) {
        int currentPage = pageBean.getCurrentPage();
        int pageSize = pageBean.getPageSize();
        DetachedCriteria detachedCriteria = pageBean.getDetachedCriteria();
        //总数据量----select count(*) from bc_staff
        //改变Hibernate框架发出的sql形式
        detachedCriteria.setProjection(Projections.rowCount());//select count(*) from bc_staff
        List<Long> list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
        Long total = list.get(0);
        pageBean.setTotal(total.intValue());//设置总数据量
        detachedCriteria.setProjection(null);//修改sql的形式为select * from ....
        //重置表和类的映射关系
        detachedCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY);
        //当前页展示的数据集合
        int firstResult = (currentPage - 1) * pageSize;
        int maxResults = pageSize;
        List rows = this.getHibernateTemplate().findByCriteria(detachedCriteria, firstResult, maxResults);
        pageBean.setRows(rows);
    }

 

 效果:

 

posted @ 2017-06-17 01:11  雪山上的蒲公英  阅读(517)  评论(0编辑  收藏  举报
/* 返回顶部代码 */