重构Struts2 JSP分页.doc

上传人:11****ws 文档编号:3135676 上传时间:2019-05-22 格式:DOC 页数:5 大小:26.50KB
下载 相关 举报
重构Struts2 JSP分页.doc_第1页
第1页 / 共5页
重构Struts2 JSP分页.doc_第2页
第2页 / 共5页
重构Struts2 JSP分页.doc_第3页
第3页 / 共5页
重构Struts2 JSP分页.doc_第4页
第4页 / 共5页
重构Struts2 JSP分页.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、前两天的 Struts2 JSP 分页由于过于关注框架实现,导致结构比较混乱。经过一些改动,再次发布。环境是 JDK1.6+mysql5.0+jboss4.0+struts 2.0.11已经实现上一版没实现的功能。首先来看 UML 图,为了简洁性,其中的 setter privateinttotalPages; privatebooleanhasNext =false; privatebooleanhasPre = false; private ArrayList pageContentList; private ArrayList indexList private PageRetrieva

2、l pr; public String execute() init(); returnSUCCESS; privatevoid init() pr = new PageRetrieval(currentPage); setPageContentList(pr.getPageContentList(); setndexList(pr.getIndexList(); setHasNext(pr.getHasNext(); setHasPre(pr.getHasPre(); setTotalPages(pr.getTotalPages(); /other getters and setters p

3、ublicclass PageRetrieval private PageInformation pi; public PageRetrieval(int currentPage) pi = new PageInformationFactory().create(currentPage); publicint getTotalPages() returnpi.getPti().getTotalPages(); /other getters and setters publicclass PageInformationFactory private DatabaseServices dataSe

4、rv; public PageInformationFactory() dataServ = MyDatabaseServices.getInstance(); public PageInformation create(int currentPage) PageInformation pi = new PageInformation(); PageTotalInfo pti = getNewPageTotalInfo(); pi.setPti(pti); if(currentPage indexTemp = getIndexList(currentPage,pti.getTotalPages

5、(); pi.setIndexList(indexTemp); return pi; 本文来自哈客部落 Hake.cc private PageTotalInfo getNewPageTotalInfo() int pageSize = 20; int totalRows = (MyDatabaseServices)dataServ).getRowCount(); int totalPages = (totalRows + pageSize-1)/pageSize; returnnew PageTotalInfo(pageSize,totalPages,totalRows); private

6、ArrayList getIndexList(int currentPage,int totalPages) int up = 0; if(currentPage+20) result = new ArrayList(); for(int i=currentPage ;i indexList; private PageTotalInfo pti; /other getters and setters publicclass MyDatabaseServices implements DatabaseServices private DataSource ds; private InitialC

7、ontext ic; private Connection conn; private PreparedStatement ps; private ResultSet rs; privatestatic MyDatabaseServices dgs = new MyDatabaseServices(); private MyDatabaseServices()/use singleton pattern, so the constructor is private try ic = new InitialContext (); ds = (DataSource)ic.lookup(“java:

8、jdbc/jsp“);/get database source catch(NamingException e) public Connection getConnection() try returnds.getConnection(); catch(SQLException e) e.printStackTrace(); returnnull; public void closeConnection(ResultSet rs,PreparedStatement ps,Connection conn) try if(rs!=null) rs.close(); if(ps!=null) ps.

9、close(); if(conn!=null) conn.close(); catch(SQLException e ) e.printStackTrace(); public ArrayList getPageContent(int currentPage,int pageSize) ArrayList list=new ArrayList(); conn = getConnection(); try ps = conn.prepareStatement(“SELECT * FROM jsptest LIMIT ?,?“); int temp = (currentPage-1)*20; ps

10、.setInt(1, temp); ps.setInt(2, pageSize); rs = ps.executeQuery(); while (rs.next() User user = new User(); user.setId(rs.getString(1); user.setName(rs.getString(2); list.add(user); return list; catch(SQLException e) e.printStackTrace(); finally dgs.closeConnection(rs, ps, conn); returnnull; publicin

11、t getRowCount() conn = getConnection(); try ps = conn.prepareStatement(“SELECT * FROM jsptest“); rs = ps.executeQuery(); rs.last(); int result = rs.getRow(); rs.first(); return result; 本文来自哈客部落 Hake.cc public Connection getConnection() try returnds.getConnection(); catch(SQLException e) e.printStack

12、Trace(); returnnull; public void closeConnection(ResultSet rs,PreparedStatement ps,Connection conn) try if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(conn!=null) conn.close(); catch(SQLException e ) e.printStackTrace(); public ArrayList getPageContent(int currentPage,int pageSize) ArrayList l

13、ist=new ArrayList(); conn = getConnection(); try ps = conn.prepareStatement(“SELECT * FROM jsptest LIMIT ?,?“); int temp = (currentPage-1)*20; ps.setInt(1, temp); ps.setInt(2, pageSize); rs = ps.executeQuery(); while (rs.next() User user = new User(); user.setId(rs.getString(1); user.setName(rs.getS

14、tring(2); list.add(user); return list; catch(SQLException e) e.printStackTrace(); finally dgs.closeConnection(rs, ps, conn); returnnull; publicint getRowCount() conn = getConnection(); try ps = conn.prepareStatement(“SELECT * FROM jsptest“); rs = ps.executeQuery(); rs.last(); int result = rs.getRow(); rs.first(); return result; 本文来自哈客部落 Hake.cc文章来自:哈客部落详文参考:http:/www.hake.cc/a/biancheng/web/jsp/2012/0709/58260_3.html

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。