Java分页代码(JFIS)

TpageList类
package jfis.util;

import java.util.List;

publicclass TPageList {
        private TPages page=null;         //分页对象
        private List objectList=null;    //对象集合
        private String pageStr="";      //分页字符串
       
        public List getObjectList() {
                returnobjectList;
        }
        publicvoid setObjectList(List objectList) {
                this.objectList = objectList;
        }
       
        public TPages getPage() {
                returnpage;
        }
        publicvoid setPage(TPages page) {
                this.page = page;
                pageStr=page.getLastPageBreak();
        }
        public String getPageStr() {
                returnpageStr;
        }
        publicvoid setPageStr(String pageStr) {
                this.pageStr = pageStr;
        }

}
Tpages类
package jfis.util;

import javax.servlet.http.HttpServletRequest;

publicclass TPages {
        private String fileName="";     
        privateintpageSize=2;           //页大小
        privateintallPage=1;             //总页数
        privateintcPage=1;               //当前页
        privateintsPage=1;               //当前页开始记录数
        privateintallRecord=1;          //总记录数
        private String lastPageBreak="";   //分页字符串
        privateinta ;//分拆list结果集的变量(相当与游标)。可从list中的确定位置开始读数据
       
       
          
//              String bian = request.getParameter("aaa")==null?"0":"5";
//              this.a = new Integer(bian);
       
               
        publicint getA() {
                returna;
        }

        publicvoid setA(int a) {
                this.a = a;
        }

        public TPages(){
               
        }
       
        public TPages(String fileName,HttpServletRequest request){
               
                this.fileName=fileName;
                //获得当前页
                String cPageStr = request.getParameter("cpage")==null?"1":request.getParameter("cpage");
                this.a = request.getParameter("aaa")==null ? 0 : new Integer(request.getParameter("aaa"));
                int cPageInt = 0;
                try
                {
                        cPageInt = Integer.parseInt(cPageStr);
                }catch(Exception e){
                        System.out.println("com.s6.util.Pages = "+e.getMessage());
                        cPageInt = 1;
                }finally{
                        cPage = cPageInt;
                }
        }
       
        //计算总页数,判断当前页的合法性,计算当前页的开始记录数,生成分页字符串
        publicvoid doPage(){
                //计算总页数
                this.allPage=(int)(this.allRecord+this.pageSize-1)/this.pageSize;
               
                //判断当前页的合法性
                if(this.cPage<0){
                        this.cPage=1;
                }
                if(this.cPage>this.allPage){
                        this.cPage=this.allPage;
                }
               
                //计算当前页开始记录数
                this.sPage=(this.cPage-1)*this.pageSize+1;
               
               
               
                //生成分页字符串
                StringBuffer str=new StringBuffer();
                //判断是否有问号“?”
                if(this.fileName.indexOf("?")==-1){
                        this.fileName=this.fileName+"?1=1";
                }
               
                str.append("<table width=\"100%\" height=\"100%\">");
               
                //添加表单
                str.append("<form name=\"pagelist\" method=\"post\" action=\"");
                str.append(this.fileName+"\">");
               
                str.append("<tr><td width=\"100%\" height=\"100%\" align=\"right\" valign=\"middle\">");
               
                str.append("总纪录数"+this.allRecord+"条");
                str.append("&nbsp;");
                //首页
                if(this.cPage>1){
                        str.append("<a href="+this.fileName+"&cpage=1>首页</a>");
                }else{
                        str.append("首页");
                }
                str.append("&nbsp;");
               
                //上一页
                if(this.cPage>1){
                        str.append("<a href=");
                        str.append(this.fileName+"&cpage=");
                        str.append(this.cPage-1);
                        str.append("&aaa=");
                        str.append(this.a-5);
                        str.append(">上一页</a>");
                }else{
                        str.append("上一页");
                }
                str.append("&nbsp;");
               
                //下一页
                if(this.cPage<this.allPage){
                        str.append("<a href=");
                        str.append(this.fileName+"&cpage=");
                        str.append(this.cPage+1);
                        str.append("&aaa=");
                        str.append(this.a+5);
                        str.append(">下一页</a>");
                }else{
                        str.append("下一页");
                }
                str.append("&nbsp;");
               
                //末页
                if(this.cPage<this.allPage){
                        str.append("<a href=");
                        str.append(this.fileName+"&cpage=");
                        str.append(this.allPage);
                        str.append(">末页</a>");
                }else{
                        str.append("末页");
                }
                str.append("&nbsp;");
               
                //第几页
                str.append("第");
                str.append(this.cPage);
                str.append("页");
                str.append("&nbsp;");
               
                //共几页
                str.append("共");
                str.append(this.allPage);
                str.append("页");
                str.append("&nbsp;");
               
                //跳转页
                for(int i=1;i<=this.allPage;i++){
                        str.append("&nbsp;");
                       
                        if(i==this.cPage){
                                str.append(i);
                        }else{
                                str.append("<a href=");
                                str.append(this.fileName+"&cpage=");
                                str.append(i);
                                str.append(">"+i+"</a>");
                        }
                }
               
                str.append("</td></tr></form>");
                str.append("</table>");
               
                lastPageBreak=str.toString();
       
        }
       
               
       
        publicint getAllPage() {
                returnallPage;
        }
        publicvoid setAllPage(int allPage) {
                this.allPage = allPage;
        }
        publicint getCPage() {
                returncPage;
        }
        publicvoid setCPage(int page) {
                cPage = page;
        }
        public String getFileName() {
                returnfileName;
        }
        publicvoid setFileName(String fileName) {
                this.fileName = fileName;
        }
        public String getLastPageBreak() {
                returnlastPageBreak;
        }
        publicvoid setLastPageBreak(String lastPageBreak) {
                this.lastPageBreak = lastPageBreak;
        }
        publicint getPageSize() {
                returnpageSize;
        }
        publicvoid setPageSize(int pageSize) {
                this.pageSize = pageSize;
        }
        publicint getSPage() {
                doPage();
                returnsPage;
        }
        publicvoid setSPage(int page) {
                sPage = page;
        }
        publicint getAllRecord() {
                returnallRecord;
        }
        publicvoid setAllRecord(int allRecord) {
                this.allRecord = allRecord;
        }
       

}
DAO里的方法
// 分页
        public TPageList pageSearchlist(TPages pages, List list) {
                TPageList pageList = new TPageList();
                // 记录集总行数
                pages.setAllRecord(list.size());
                // rs用于保存从list中截取的记录
                List rs = new ArrayList();
                // 控制从list集合中的第几条开始读取,相当于游标
                int a = pages.getA();
                // for循环中"5"代表一次读出多少条数据:"a"控制从list集合中的第几条开始读取,相当于游标。
                for (int i = 0; i < 5 && a < list.size(); a++) {
                        rs.add(list.get(a));
                        i++;
                }
                Iterator iterator = rs.iterator();
                // 得到分页字符串
                pages.getSPage();
                int i = 0;// 循环变量
                // 判断记录总行数是否大于0,并循环创建USER对象
                if (pages.getAllRecord() > 0) {
                        do {
                                i++;
                        } while (iterator.hasNext() && i < pages.getPageSize());
                        pageList.setPage(pages);
                        pageList.setObjectList(rs);
                }
                return pageList;
        }
Action里的方法
public ActionForward pageSearchList(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {

                TPages pages = new TPages(
                                "/JFIS_CompanyManager/client/outer.do?method=pageSearchList",
                                request);

                pages.setPageSize(5);

                TPageList pageList = iouter.pageSearchlist(pages, iouter.getAllOuter());
                request.setAttribute("outers", pageList);

                return mapping.findForward("outer_list");

        }
SSH分页
说明:分页的两个类不变,只变Action和DAO。
Action中
// 分页
        public ActionForward pageSearchList(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {
                Pages pages = new Pages(                         "/JFIS_CompanyManager/client/outer.do?method=pageSearchList",request);
                pages.setPageSize(10);
                PageList pageList = iouter.outerList(pages);
                request.setAttribute("outers", pageList);
                return mapping.findForward("outer_list");
        }
DAO中
// 分页
        public PageList outerList(Pages pages) {
                PageList pageList = new PageList();
                List list = new ArrayList();
        /*开启一个HibernateSession对象*/
                Session session = this.getSession();
                Transaction t = session.beginTransaction();
                /*获得结果集*/
                Query query = session
                                .createQuery("from jfis.bean.JfisClientItem order by starttime desc");
                /*记录集总行数*/
                pages.setAllRecord(query.list().size());
                query.setFirstResult(pages.getSPage() - 1);
                query.setMaxResults(10);/*设置每页显示的记录数*/
                List rs = query.list();
                Iterator iterator = rs.iterator();
                /*得到分页字符串*/
                pages.getSPage();
                int i = 0;/*循环变量*/
                /*判断记录总行数是否大于0,并循环创建USER对象*/
                if (pages.getAllRecord() > 0) {
                        do {
                                list = rs;
                                i++;
                        } while (iterator.hasNext() && i < pages.getPageSize());
                        pageList.setPage(pages);
                        pageList.setObjectList(list);           
                        t.commit();/*事务提交*/
                        session.close();
                }
                return pageList;
        }

你可能感兴趣的:(java,DAO,bean,servlet,ssh)