前兩天的Struts2 + JSP分頁由于過于關(guān)注框架實(shí)現(xiàn),導(dǎo)致結(jié)構(gòu)比較混亂。經(jīng)過一些改動(dòng),再次發(fā)布。
環(huán)境是JDK1.6+mysql5.0+jboss4.0+struts 2.0.11
已經(jīng)實(shí)現(xiàn)上一版沒實(shí)現(xiàn)的功能。
首先來看UML圖,為了簡(jiǎn)潔性,其中的setter & getter并沒有標(biāo)出。 public classShowActionextendsActionSupport{ privateintcurrentPage = 1; privateinttotalPages; privatebooleanhasNext =false; privatebooleanhasPre = false; private ArrayList pageContentList; private ArrayList<PageIndex> indexList; private PageRetrieval pr; public String execute(){ init(); returnSUCCESS; } privatevoid init(){ pr = new PageRetrieval(currentPage); setPageContentList(pr.getPageContentList()); setIndexList(pr.getIndexList()); setHasNext(pr.getHasNext()); setHasPre(pr.getHasPre()); setTotalPages(pr.getTotalPages()); } //other getters and setters } publicclass 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 dataServ; public PageInformationFactory(){ dataServ = MyDatabaseServices.getInstance(); } public PageInformation create(int currentPage){ PageInformation pi = new PageInformation(); PageTotalInfo pti = getNewPageTotalInfo(); pi.setPti(pti); if(currentPage < pti.getTotalPages()){ pi.setHasNext(true); } if(currentPage !=1){ pi.setHasPre(true); } pi.setPageContentList(((MyDatabaseServices)dataServ).getPageContent(currentPage, pti.getPageSize())); ArrayList<PageIndex> indexTemp = getIndexList(currentPage,pti.getTotalPages()); pi.setIndexList(indexTemp); return pi; } private PageTotalInfo getNewPageTotalInfo(){ int pageSize = 20; int totalRows = ((MyDatabaseServices)dataServ).getRowCount(); int totalPages = (totalRows + pageSize-1)/pageSize; returnnew PageTotalInfo(pageSize,totalPages,totalRows); } private ArrayList<PageIndex> getIndexList(int currentPage,int totalPages){ int up = 0; if((currentPage+20)<=totalPages){ up = currentPage+20; } else {up = totalPages+1;} ArrayList<PageIndex> result = new ArrayList<PageIndex>(); for(int i=currentPage ;i<up; i++){ PageIndex temp = new PageIndex(i); result.add(temp); } return result; } } publicclass PageInformation { privateintcurrentPage; privatebooleanhasNext = false; privatebooleanhasPre = false; private ArrayList pageContentList; private ArrayList<PageIndex> indexList; private PageTotalInfo pti; //other getters and setters } publicclass MyDatabaseServices implements DatabaseServices{ private DataSource ds; private InitialContext 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:jdbc/jsp");//get database source }catch(NamingException e){ e.printStackTrace(); } } 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.close(); } if(conn!=null){ conn.close(); } }catch(SQLException e ){ e.printStackTrace(); } } public ArrayList<User> getPageContent(int currentPage,int pageSize){ ArrayList<User> list=new ArrayList<User>(); 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.getString(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; }catch(SQLException e){ e.printStackTrace(); }finally{ dgs.closeConnection(rs, ps, conn); } return 0; } publicstaticsynchronized MyDatabaseServices getInstance()//get the sigleton instance { if(null==dgs){ dgs= new MyDatabaseServices(); } returndgs; } } PageIndex 和PageTotalInfo 只要對(duì)照UML圖加上set和get方法就可以了。 代碼可能有點(diǎn)亂,對(duì)照UML圖看吧。 如果要重用,只要改變JSP頁面,以及下面的MyDatabaseServices的具體實(shí)現(xiàn),還有就是把USER替換成你需要顯示的數(shù)據(jù)。 最后以下是JSP頁面代碼。 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:form action="ShowAction" method="GET"> <h1>Welcome</h1><BR> <h1>CurrentPage <s:property value="currentPage"/></h1> <!--show items of this page--> <s:iterator value="pageContentList" status="status"> <s:property value="id"/> <s:property value="name"/> <BR> </s:iterator> <!--define the url of the previous page and next page--> <s:url id="url_pre" value="ShowAction.action"> <s:param name="currentPage" value="currentPage-1"></s:param> </s:url> <s:url id="url_next" value="ShowAction.action"> <s:param name="currentPage" value="currentPage+1"></s:param> </s:url> <s:url id="url_first" value="ShowAction.action"> <s:param name="currentPage" value="1"></s:param> </s:url> <s:url id="url_last" value="ShowAction.action"> <s:param name="currentPage" value="totalPages"></s:param> </s:url> <!-- use url defined above --> <s:a href ="%{url_first}">First Page</s:a> <s:if test="hasPre"> <s:a href="%{url_pre}">Pre</s:a> </s:if> <s:iterator value="indexList" status="status"> <s:url id="url" value="ShowAction.action"> <!-- pass the currentPage parameter --> <s:param name="currentPage" value="indexNumber"></s:param> </s:url> <s:a href="%{url}"><s:property value="indexNumber"/> </s:a> </s:iterator> <s:if test="hasNext"> <s:a href="%{url_next}">Next</s:a> </s:if> <s:a href ="%{url_last}">Last Page</s:a> </s:form> </body> </html> 可能還有不盡人意之處,大家多多指點(diǎn),一起進(jìn)步。
接下來是各類代碼
聯(lián)系客服