Struts分页控制

转载 2004年09月09日 21:47:00
看到大家对struts的学习热情很高,而且这方面的资料很少,看着大家很累,所以发发善心,看到很好的一种分页方法,就贴出来大家共同分享!(原作在javaworld),有不完善的地方,希望大家改正!!!
使用一个页面控制类,它记录页面信息,如上页,下页,当前页等。在查询的Action中,将这个控制类和查询条件一起传递给数据库访问bean,然后将这两个参数保存在用户session中。在分页控制Action中,利用接收到的分页参数调用数据库访问的bean.

(1)分页控制类

package com.tower.util;
public class PageController {
    int totalRowsAmount; //总行数
    boolean rowsAmountSet; //是否设置过totalRowsAmount
    int pageSize=2; //每页行数
    int currentPage=1; //当前页码
    int nextPage;
    int previousPage;
    int totalPages; //总页数
    boolean hasNext; //是否有下一页
    boolean hasPrevious; //是否有前一页
    String description;
    int pageStartRow;
    int pageEndRow;

    public PageController(int totalRows){
        setTotalRowsAmount(totalRows);
    }
    public PageController(){}
    public void setTotalRowsAmount(int i) {
        if(!this.rowsAmountSet){
            totalRowsAmount = i;
            totalPages=totalRowsAmount/pageSize+1;
            setCurrentPage(1);
            this.rowsAmountSet=true;
        }
    }
    public void setCurrentPage(int i) {
        currentPage = i;
        nextPage=currentPage+1;
        previousPage=currentPage-1;
        //计算当前页开始行和结束行
        if(currentPage*pageSize<totalRowsAmount){
            pageEndRow=currentPage*pageSize;
            pageStartRow=pageEndRow-pageSize+1;
        }else{
            pageEndRow=totalRowsAmount;
            pageStartRow=pageSize*(totalPages-1)+1;
        }
        if (nextPage>totalPages){
            hasNext=false;
        }else{
            hasNext=true;
        }
        if(previousPage==0){
            hasPrevious=false;
        }else{
            hasPrevious=true;
        }
        System.out.println(this.description());
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public boolean isHasNext() {
        return hasNext;
    }
    public boolean isHasPrevious() {
        return hasPrevious;
    }
    public int getNextPage() {
        return nextPage;
    }
    public int getPageSize() {
        return pageSize;
    }
    public int getPreviousPage() {
        return previousPage;
    }
    public int getTotalPages() {
        return totalPages;
    }
    public int getTotalRowsAmount() {
        return totalRowsAmount;
    }
    public void setHasNext(boolean b) {
        hasNext = b;
    }
    public void setHasPrevious(boolean b) {
        hasPrevious = b;
    }
    public void setNextPage(int i) {
        nextPage = i;
    }
    public void setPageSize(int i) {
        pageSize = i;
    }
    public void setPreviousPage(int i) {
        previousPage = i;
    }
    public void setTotalPages(int i) {
        totalPages = i;
    }
    public int getPageEndRow() {
        return pageEndRow;
    }
    public int getPageStartRow() {
        return pageStartRow;
    }
    public String getDescription(){
        String description="Total:"+this.getTotalRowsAmount()+" items "+this.getTotalPages() +" pages";
    // this.currentPage+" Previous "+this.hasPrevious +
    // " Next:"+this.hasNext+
    // " start row:"+this.pageStartRow+
    // " end row:"+this.pageEndRow;
        return description;
    }
    public String description(){
        String description="Total:"+this.getTotalRowsAmount()+
            " items "+this.getTotalPages() +" pages,Current page:"+
        this.currentPage+" Previous "+this.hasPrevious +
            " Next:"+this.hasNext+
            " start row:"+this.pageStartRow+
            " end row:"+this.pageEndRow;
        return description;
    }
    public static void main(String args[]){
        PageController pc=new PageController(3);
        System.out.println(pc.getDescription());
        // pc.setCurrentPage(2);
        // System.out.println(pc.description());
        // pc.setCurrentPage(3);
        // System.out.println(pc.description());
    }

(2)查询Action的代码片断

    public ActionForward execute(
                        ActionMapping mapping,
                        ActionForm form,
                        HttpServletRequest request,
                        HttpServletResponse response)
                        throws Exception {
        Base queryForm= (Base) form;
        if(!queryForm.getName().equals("")){
            PageController pc=new PageController();
            EmployeeBase service=new EmployeeBase();
            ArrayList result=(ArrayList)service.search(queryForm,pc);
            HttpSession session=request.getSession();
            session.setAttribute("queryForm",queryForm);
            session.setAttribute("pageController",service.getPageController());
            request.setAttribute("queryResult",result);
            request.setAttribute("pageController",service.getPageController());
            return mapping.findForward("haveResult");
        }else{
            return mapping.findForward("noResult");
        }
    }

(3)翻页Action的代码片断

    public ActionForward execute(
                ActionMapping mapping,
                ActionForm form,
                HttpServletRequest request,
                HttpServletResponse response)
                throws Exception {
        //读取翻页参数
        TurnPageForm turnPageForm=(TurnPageForm)form;
        //从PageController中取出查询信息,并使用bean提供的调用接口处理结果
        HttpSession session=request.getSession();
        PageController pc=(PageController)session.getAttribute("pageController");
        Base queryForm=(Base)session.getAttribute("queryForm");
        pc.setCurrentPage(turnPageForm.getViewPage());
        EmployeeBase service=new EmployeeBase();
        ArrayList result=(ArrayList)service.search(queryForm,pc);
        //根据参数将数据写入 request
        request.removeAttribute("queryResult");
        request.removeAttribute("pageController");
        request.setAttribute("queryResult",result);
        request.setAttribute("pageController",pc);
        //forward 到显示页面
        return mapping.findForward("haveResult");
    }

(4)数据库访问bean中的片断

    public Collection search(Base base, PageController pc)throws SQLException {
        ArrayList emps = new ArrayList();
        ResultSet rs = getSearchResult(base);
        rs.absolute(-1);
        pc.setTotalRowsAmount(rs.getRow());
        setPageController(pc);
        if (rs.getRow() > 0) {
            rs.absolute(pc.getPageStartRow());
            do {
                System.out.println("in loop" + rs.getRow());
                Base b = new Base();
                b.setName(rs.getString("Name"));
                b.setIdCard(rs.getString("IDCard"));
                System.out.println("From db:" + rs.getString("IDCard"));
                emps.add(b);
                if (!rs.next()) {
                    break;
                }
            } while (rs.getRow() < (pc.getPageEndRow() + 1));
        }
            return emps;
    }

(5)在jsp中,翻页部分的代码片断

<bean:write name="pageController" property="description"/>
<logic:equal name="pageController" property="hasPrevious" value="true">
    <a href="turnPage.do?viewPage=<bean:write name="pageController" property="previousPage"/>" class="a02">
        Previous
    </a>
</logic:equal>
<logic:equal name="pageController" property="hasNext" value="true">
    <a href="turnPage.do?viewPage=<bean:write name="pageController" property="nextPage"/>" class="a02">
        Next
    </a>
</logic:equal>

这样一来,翻页的功能可以以你喜欢的方式表现给client

struts2中的跳转详解

COME FROM Struts2的ResultType和Action处理链  Struts2的结果类型 在struts-default.xml中定义了Struts2内置的所...
  • u011554611
  • u011554611
  • 2015年01月12日 23:43
  • 3068

JAVA 开发平台的技术和框架(二)前端控制器:Struts2 ,Spring MVC

前端控制器:Struts2 ,Spring MVC 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响...
  • liushuicike
  • liushuicike
  • 2015年03月20日 13:17
  • 2086

Struts对MVC的实现流程

文章的主题是Struts对MVC的实现流程,那么,首先要解决的问题是:什么是MVC?     MVC是Model—View—Controler的简称。即模型—视图—控制器。MVC是一种设计模式,它...
  • sunqing0316
  • sunqing0316
  • 2015年09月28日 15:51
  • 1710

Struts+Hibernate实现分页

  • 2015年05月12日 21:24
  • 32KB
  • 下载

struts增删改查(分页)

  • 2014年04月18日 11:34
  • 1.37MB
  • 下载

纯struts分页小例子

  • 2009年02月09日 10:59
  • 882KB
  • 下载

MyBBS(struts和hibernate版及分页类设计)

  • 2012年08月06日 10:42
  • 1.07MB
  • 下载

struts2+spring+hibernate分页,事务,自定义切面

  • 2009年07月17日 14:29
  • 40KB
  • 下载

基于struts2 自定义标签 分页

  • 2010年11月28日 14:08
  • 5.41MB
  • 下载

hibernate,struts,spring,JSP,实现分页技术

  • 2010年10月27日 10:28
  • 78KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Struts分页控制
举报原因:
原因补充:

(最多只允许输入30个字)