日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Struts 2 JSP分頁,重構版

 pengx 2008-05-25

前兩天的Struts2 + JSP分頁由于過于關注框架實現(xiàn),導致結構比較混亂。經(jīng)過一些改動,再次發(fā)布。

環(huán)境是JDK1.6+mysql5.0+jboss4.0+struts 2.0.11

已經(jīng)實現(xiàn)上一版沒實現(xiàn)的功能。

首先來看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());

    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 只要對照UML圖加上setget方法就可以了。

代碼可能有點亂,對照UML圖看吧。

如果要重用,只要改變JSP頁面,以及下面的MyDatabaseServices的具體實現(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"/>&nbsp</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>

可能還有不盡人意之處,大家多多指點,一起進步。

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多