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