前两天的Struts2 JSP分页由于过于关注框架实现,导致结构比较混乱。经过一些改动,再次发布。
环境是JDK1.6+mysql5.0+jboss4.0+struts 2.0.11
已经实现上一版没实现的功能。
首先来看UML图,为了简洁性,其中的setter & getter并没有标出。
- 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());
- setndexList(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;}
- ArayList<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 只要对照UML图加上set和get方法就可以了。
代码可能有点乱,对照UML图看吧。
如果要重用,只要改变Struts2 JSP页面,以及下面的MyDatabaseServices的具体实现,还有就是把USER替换成你需要显示的数据。
最后以下是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>
以上是Struts2 JSP分页可能还有不尽人意之处,大家多多指点,一起进步。