国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Struts 2 JSP分頁,重構(gòu)版

前兩天的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圖加上setget方法就可以了。

代碼可能有點(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"/>&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>

可能還有不盡人意之處,大家多多指點(diǎn),一起進(jìn)步。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
struts2 JSP 實(shí)現(xiàn)分頁顯示
我的Struts分頁方法
JR - 精品文章 - 在struts中分頁的一種實(shí)現(xiàn)
WEB開發(fā)中Struts分頁算法
MyBatis分頁
tree
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服