Struts分页

1、分页的bean类PaginationSupport.java
  2、写好后直接在action里面调用,计算当前页显示的数据
  3、写一个公用的jsp页面,直接在需要分页的页面include就可以了。。
  4、运行后式样
  

package com.jrosion.common.util;

import java.util.ArrayList;
import java.util.Vector;

/**
 * Struts分页
 *
 * @author Jrosion
 * @version 1.0
 * @serialData 2006-11-30
 */
public class PaginationSupport {

    // 每页显示的记录数
    public final static int PAGE_SIZE = 12;

    private int currentPage = 1; // 当前页

    private int totalPags = 0; // 总页数

    private int totalRows = 0; // 总数据条数

    private int pageStartRow; // 每页起始行

    private int pageEndRow; // 每页结束行

    private boolean isntHaveNextPage = false; // 是否有下一页

    private boolean isntHavePreviousPage = false; // 是否有上一页

    private Vector navigationPages = new Vector();

    private ArrayList currentPageDates = new ArrayList(); // 当前页数据

    private String url = ""; // 链接地址

    private int nextPage; // 下一页

    private int previousPage; // 上一页

    private String condition;

    /**
     * 构造分页模型
     *
     * @param date分页数据
     */
    public PaginationSupport(ArrayList paginationDate, int currentPage,
            String url) {

        if (paginationDate != null) {

            // 总页数
            this.totalRows = paginationDate.size();

            // 计算总页数
            this.totalPags = this.getTotalPages(totalRows);

            // 当前页
            if (currentPage <= 0) {
                this.currentPage = 1;
            } else if (currentPage > totalPags) {
                this.currentPage = totalPags;
            } else {
                this.currentPage = currentPage;
            }

            // 上一页
            this.previousPage = currentPage - 1;

            // 下一页
            this.nextPage = currentPage + 1;

            // 判断是否还有下页
            this.isntHaveNextPage = this.getIsntHaveNextPage(currentPage,
                    totalPags);

            // 判断是否有上页
            if (currentPage > 1) {
                this.isntHavePreviousPage = true;
            } else {
                this.isntHavePreviousPage = false;
            }

            // 设置数据起始行
            int[] row = this.getStartAndEndRow(paginationDate, currentPage,
                    totalPags, totalRows);
            this.pageStartRow = row[0];
            this.pageEndRow = row[1];

            // 获取当前页需要显示的数据
            this.currentPageDates = this.getCurrentPageDate(paginationDate,
                    pageStartRow, pageEndRow);

            this.url = url;

            // 设置导航页
            this.navigationPages = this.getNavigationPages(currentPage,
                    totalPags);
        }
    }

    /**
     * 根据记录条数进行构造
     *
     * @param totalRecord
     * @param currentPage
     * @param url
     */
    public PaginationSupport(int totalRecord, int currentPage, String url) {

        if (totalRecord > 0) {

            // 总页数
            this.totalRows = totalRecord;

            // 计算总页数
            this.totalPags = this.getTotalPages(totalRows);

            // 当前页
            if (currentPage <= 0) {
                this.currentPage = 1;
            } else if (currentPage > totalPags) {
                this.currentPage = totalPags;
            } else {
                this.currentPage = currentPage;
            }

            // 上一页
            this.previousPage = currentPage - 1;

            // 下一页
            this.nextPage = currentPage + 1;

            // 判断是否还有下页
            this.isntHaveNextPage = this.getIsntHaveNextPage(currentPage,
                    totalPags);

            // 判断是否有上页
            if (currentPage > 1) {
                this.isntHavePreviousPage = true;
            } else {
                this.isntHavePreviousPage = false;
            }

            // 设置数据起始行
            int[] row = this.getStartAndEndRow(currentPage);
            this.pageStartRow = row[0];
            this.pageEndRow = row[1];

            this.url = url;

            // 设置导航页
            this.navigationPages = this.getNavigationPages(currentPage,
                    totalPags);
        }
    }

    /**
     * 计算总页数
     *
     * @param totalRows
     * @return
     */
    public int getTotalPages(int totalRows) {

        int result = 1;

        if (totalRows > PAGE_SIZE) {
            if ((totalRows % PAGE_SIZE) == 0) {
                result = totalRows / PAGE_SIZE;
            } else {
                result = totalRows / PAGE_SIZE + 1;
            }
        }

        return result;
    }

    /**
     * 判断是否有下一页
     *
     * @param currentPages
     * @param totalPages
     * @return
     */
    public boolean getIsntHaveNextPage(int currentPages, int totalPages) {

        boolean result = false;

        if (currentPage >= totalPags) {
            result = false;
        } else {
            result = true;
        }

        return result;
    }

    /**
     * 返回当前页显示数据的起始行
     *
     * @param currentPage当前页
     * @param totalPags总页数
     * @param totalRows总行数
     * @return
     */
    public int[] getStartAndEndRow(ArrayList paginationDate, int currentPage,
            int totalPags, int totalRows) {

        int[] result = new int[2];

        int startRow = PAGE_SIZE * (currentPage - 1);

        int endRow = 0;

        if (currentPage == totalPags) {
            endRow = totalRows - 1;
        } else {
            endRow = startRow + PAGE_SIZE - 1;
        }

        result[0] = startRow;

        result[1] = endRow;

        return result;

    }

    /**
     * 返回当前页显示数据的起始行
     *
     * @param currentPage
     * @return
     */
    public int[] getStartAndEndRow(int currentPage) {

        int[] result = new int[2];

        int startRow = PAGE_SIZE * (currentPage - 1);

        int endRow = PAGE_SIZE;

        result[0] = startRow;

        result[1] = endRow;

        return result;

    }

    /**
     * 初始化导航页面
     *
     * @return
     */
    public Vector initNavigationPages(int totalPages) {

        Vector result = new Vector();

        int loop = 11;

        if (totalPages < 10) {
            loop = totalPages + 1;
        }

        for (int i = 1; i < loop; i++) {
            String element = String.valueOf(i).toString();
            result.addElement(element);
        }

        return result;
    }

    /**
     * 得到导航页面
     *
     * @param currentPages
     * @param totalPages
     * @return
     */
    public Vector getNavigationPages(int currentPages, int totalPages) {

        Vector result = new Vector();

        if (currentPages > totalPages) {
            currentPages = totalPages;
        }

        if (currentPages <= 0) {
            currentPages = 1;
        }

        if (totalPages > 10) {
            int endPages = currentPages + 10;
            int startPages = currentPages;
            if (endPages > totalPages) {
                int morePages = endPages - totalPages;
                startPages = currentPages - morePages + 1;
                endPages = totalPages + 1;
            }

            for (int i = startPages; i < endPages; i++) {
                String element = String.valueOf(i).toString();
                result.addElement(element);
            }

        } else {
            result = this.initNavigationPages(totalPages);
        }

        return result;
    }

    /**
     * 得到当前页面数据
     *
     * @param startRow开始行索引
     * @param endRow结束行索引
     * @return
     */
    public ArrayList getCurrentPageDate(ArrayList pageDates, int startRow,
            int endRow) {

        if (pageDates == null || pageDates.size() == 0) {
            return null;
        }

        ArrayList result = new ArrayList();

        try {
            for (int i = startRow; i < endRow + 1; i++) {
                result.add(pageDates.get(i));
            }
        } catch (IndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }

        return result;

    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public boolean isIsntHavePreviousPage() {
        return isntHavePreviousPage;
    }

    public void setIsntHavePreviousPage(boolean isntHavePreviousPage) {
        this.isntHavePreviousPage = isntHavePreviousPage;
    }

    public int getPageEndRow() {
        return pageEndRow;
    }

    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }

    public int getPageStartRow() {
        return pageStartRow;
    }

    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
    }

    public int getTotalPags() {
        return totalPags;
    }

    public void setTotalPags(int totalPags) {
        this.totalPags = totalPags;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public ArrayList getCurrentPageDates() {
        return currentPageDates;
    }

    public void setCurrentPageDates(ArrayList currentPageDates) {
        this.currentPageDates = currentPageDates;
    }

    public boolean isIsntHaveNextPage() {
        return isntHaveNextPage;
    }

    public void setIsntHaveNextPage(boolean isntHaveNextPage) {
        this.isntHaveNextPage = isntHaveNextPage;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getPreviousPage() {
        return previousPage;
    }

    public void setPreviousPage(int previousPage) {
        this.previousPage = previousPage;
    }

    public Vector getNavigationPages() {
        return navigationPages;
    }

    public void setNavigationPages(Vector navigationPages) {
        this.navigationPages = navigationPages;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

}

}

/******************************************/
        service部分代码
/*******************************************/

    public int count(String condition) {
        if (condition == "" || condition == null || condition.equals("null")) {
            condition = " 1=1 ";
        }
        return teacherDAO.count(condition);
    }

    public List query(String condition, int startRow, int displayRows) {
        if (condition == "" || condition == null || condition.equals("null")) {
            condition = " 1=1 ";
        }
        return teacherDAO.query(condition, startRow, displayRows);
    }

    public HashMap pagination(String condition, String currentPage) {

        HashMap result = new HashMap();

        if (currentPage == "" || currentPage == null
                || currentPage.equals("null")) {
            currentPage = "1";
        }

        int page = Integer.valueOf(currentPage).intValue();

        int recordCount = this.count(condition);

        PaginationSupport pagination = new PaginationSupport(recordCount, page,
                "/displayTeacher.do");

        List pageDate = this.query(condition, pagination.getPageStartRow(),
                pagination.getPageEndRow());

        result.put("pageData", pageDate);

        result.put("navigation", pagination);

        return result;
    }
。。。。。

-----------------------------------action调用---------------------

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {

        List result = teacherService.query();

        String page = request.getParameter("page");

        if (page == null || page.equals("")) {
            page = "1";
        }

        int p = Integer.valueOf(page).intValue();

        PaginationSupport pagination = new PaginationSupport(
                (ArrayList) result, p, "/displayTeacher.do");

        result = pagination.getCurrentPageDates();

        request.setAttribute("QueryObject", result);

        request.setAttribute("page", pagination);

        return mapping.findForward("teacherIndexGo");

    }

  

---公用的jsp页面

<%@ page pageEncoding="gb2312" contentType="text/html;charset=gb2312" %>  

<bean:define id="pages" name="page"/>

<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0">
    <TR class="tr">
        <TD width="15%" align="right" height="20" valign="baseline">
            共:<bean:write name="pages" property="totalPags"/>页/
            第<bean:write name="pages" property="currentPage"/>页  
            记录总数:<bean:write name="pages" property="totalRows"/>条
           <a href="<%=request.getContextPath()%><bean:write name=‘pages‘ property=‘url‘/>?page=1">
            <img border="0" src="<%=request.getContextPath()%>/skin/<bean:write _fcksavedurl=""<%=request.getContextPath()%>/skin/<bean:write" name="user" property="skin"/>/first.gif"></a>

           <logic:equal name="pages" property="isntHavePreviousPage" value="true">
             <a href="<%=request.getContextPath()%><bean:write name=‘pages‘ property=‘url‘/>?page=<bean:write name=‘pages‘ property=‘previousPage‘/>">
              <img border="0" src="<%=request.getContextPath()%>/skin/<bean:write name="user" property="skin"/>/previous.gif"></a>
           </logic:equal>

           <logic:iterate id="navigation" name="pages" property="navigationPages">
            <a href="<%=request.getContextPath()%><bean:write name=‘pages‘ property=‘url‘/>?page=<bean:write name=‘navigation‘/>">
              <bean:write name="navigation"/></a>
           </logic:iterate>

           <logic:equal name="pages" property="isntHaveNextPage" value="true">
            <a href="<%=request.getContextPath()%><bean:write name=‘pages‘ property=‘url‘/>?page=<bean:write name=‘pages‘ property=‘nextPage‘/>">
             <img border="0" src="<%=request.getContextPath()%>/skin/<bean:write name="user" property="skin"/>/next.gif"></a>
           </logic:equal>
           <a href="<%=request.getContextPath()%><bean:write name=‘pages‘ property=‘url‘/>?page=<bean:write name=‘pages‘ property=‘totalPags‘/>">
            <img border="0" src="<%=request.getContextPath()%>/skin/<bean:write name="user" property="skin"/>/last.gif"></a>

           <input type="text" size="4" name="iputPage"><input type="button" name="go" value="Go">

        </TD>
    </TR>
</table>

  

时间: 2024-10-06 21:07:43

Struts分页的相关文章

Struts分页的一个实现

在Web应用程序里,分页总让我们开发人员感到很头疼,倒不是因为技术上有多么困难,只是本来和业务没有太多关系的这么一个问题,你却得花不少功夫来处理.要是稍不留神,时不时出点问题就更郁闷了.我现在做的一个项目也到了该处理分页的时候了,感觉以前处理得都不好,所以这次有所改变,基本目标是在现有(未分页)的代码基础上,尽量少做修改,并且同样的代码可以应用于不同模块的分页.以下就是我用的方法: 首先,考虑分页绝大多数发生在列表时,组合查询时也需要用到.在我的项目里,列表的Action一般名字为ListXXX

JQueryPagination分页插件,ajax从struts请求数据

2017-07-16 学完了struts,做了个关于分页的小例子,用到了JQuery分页插件Pagination,先贴下插件下载地址 http://www.jq22.com/jquery-info13734 插件作者对于参数讲解的不够详细,琢磨了半天才明白怎么用,不多说,直接代码 1.客户端(jsp页面) 1 /*这些css为了控制生成的数据和分页的li标签的位置*/ 2 a { 3 text-decoration:none; 4 color:black; 5 font-weight: bold

ajax+json模态框中分页(spring+struts+mybatis+easyui分页插件)

0.业务需求: 点击每个数字的时候可以显示每个对应的详细记录.也就是得点击11的时候拿着开采部与C级去查询. 1.页面中的模态框与分页组件(注意:需要隐藏一个页号,点击分页插件的时候给隐藏的页号赋值,ajax再次请求的时候取页面的页号值) <!-- 隐藏查询条件的页号 --> <input type="hidden" name="currentPage" id="currentPage"> <!-- 模态框 统计详细

jQuery+AJAX+Struts实现无刷新分页

jQuery+AJAX+Struts实现无刷新分页 说明: 1.需要jQuery插件js文件: 2.使用myeclipse添加struts能力: 从前从客户端页面向服务器发送Ajax请求,需要在js中先创建XMLHttpRequest对象,对象创建好以后使用OPEN('GET/POST',URL,同步/异步)设置提交方式,URL地址,使用同步还是异步方式.然后使用send(data)向服务器发送数据,同时使用onreadystatechange来绑定回调函数.如果是使用GET方式提交数据,那么就

Struts+Hibernate+jsp页面 实现分页

dao层数据库代码: package com.hanqi.dao; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilde

5、Spring+Struts2+MyBatis+分页(无代理)增删改查

1.创建如下项目结构 2.在src下的com.entity包下创建Dept.java 1 package com.entity; 2 /** 3 * 部门表 4 * @author Holly老师 5 * 6 */ 7 public class Dept { 8 private Integer deptno; //部门编号 9 private String dname; //部门名称 10 private String loc; //位置 11 12 13 public Dept() { 14

3、mybatis动态sql+struts2(通配符+全局配置+分页)

1.创建userinfo.sql数据库脚本 1 create table USERINFO 2 ( 3 id NUMBER not null, 4 uname VARCHAR2(20), 5 password VARCHAR2(20), 6 age NUMBER 7 ) 8 ; 9 alter table USERINFO add primary key (ID); 10 11 create sequence seq_userinfo; 12 13 insert into USERINFO (i

SSH框架--struts深入详解(一)

学习了struts,但是对于它的由来,以及为什么使用action和struts.xml的方式而不采用以前的servlet方式,有些疑问,到底之前的方式有什么弊端,struts又给我们带来了什么便利? 下面一一为大家解答! struts的由来: 随着JSP与Servlet 技术大量应用于以Web为基础的应用程序,为了提升Web 应用程序可维护性与重复使用性,Java开发人员提出了一些较佳的开发模式.比较常见的两种JSP应用架构分别为Model1 与Model 2.详情参见(JAVA学习篇--JAV

SpringMVC+easyUI中datagrid分页实现_2014.5.1

一.概述 SpringMVC: 1.是面对方法级变量的,在操作起来会比struts方便一些(structs是类级变量),具体体现在了srpingMVC的注解上面, 如@RequstMapping("/login"),而且对于返回值ModelAndView这也是一大亮点,既可以返回一个页面(View),再加上@ResponseBody注解以后就可以返回一个      模型对象(也就是一种数据结构). 2.对于方法级传入的参数操作起来也相当方便,比如本例中,在加载DataGrid时,会像后