Struts 分页显示

  1. Struts 分页显示
  2. 分页类 Page.java:
  3. public class Page {
  4.     int currentPage=1;//当前页数
  5.     int totalPages=0;//总页数
  6.     int pageRecorders=3;//每页显示数
  7.     int totalRows=0;//总数据数
  8.     int pageStartRow=0;//每页的起始数
  9.     int pageEndRow;//每页的终止数
  10.     boolean hasNextPage=false;//是否有下一页
  11.     boolean hasPreviousPage=false;//是否有前一页
  12.     List<Country> list;
  13.     public Page(){}
  14.     public Page(List<Country> list)
  15.     {
  16.         //初始化所有参数
         this.list=list;
         totalRows=list.size();
         currentPage=1;
         hasPreviousPage=false;
         totalPages=totalRows%pageRecorders==0?totalRows%pageRecorders:totalRows%pageRecorders+1;
         if(currentPage>=totalPages) {
          hasNextPage=false;
             }
             else {
                hasNextPage=true;
             }
           if(totalRows<pageRecorders) {
                 this.pageStartRow=0;
                 this.pageEndRow=totalRows;
             }
             else {
                 this.pageStartRow=0;
                 this.pageEndRow=pageRecorders;
             }
  17.     }
  18.     public void setList(List<Country> list) {
  19.         this.list = list;
  20.     }
  21.     public int getTotalRows() {
  22.         return totalRows;
  23.     }
  24.     public int getCurrentPage() {
  25.         return currentPage;
  26.     }
  27.     public boolean isHasNextPage() {
  28.         return hasNextPage;
  29.     }
  30.     public boolean isHasPreviousPage() {
  31.         return hasPreviousPage;
  32.     }
  33.     public int getTotalPages() {
  34.         return totalPages;
  35.     }
  36.     public List<Country> getList(int curpage) {
  37.         this.currentPage=curpage;
  38.         if(currentPage*pageRecorders<totalRows) {
  39.             pageEndRow=currentPage*pageRecorders;
  40.             pageStartRow=pageEndRow-pageRecorders;
  41.            }
  42.         else{
  43.             pageEndRow=totalRows;
  44.             pageStartRow=pageRecorders*(totalPages-1);
  45.           }
  46.         return list.subList(pageStartRow,pageEndRow);
  47.     }
  48.     public List<Country> getNextPage() {
  49.         currentPage=currentPage+1;
  50.         if((currentPage-1)>0) {
  51.             hasPreviousPage=true;
  52.          }
  53.         else {
  54.             hasPreviousPage=false;
  55.         }
  56.         if(currentPage>=totalPages) {
  57.             hasNextPage=false;
  58.         }
  59.         else {
  60.             hasNextPage=true;
  61.         }
  62.         List<Country> list=this.getList(currentPage);
  63.         return list;
  64.     }
  65.     public List<Country> getPreviousPage() {
  66.         currentPage=currentPage-1;
  67.         if(currentPage==0) {
  68.             currentPage=1;
  69.         }
  70.         if(currentPage>=totalPages) {
  71.             hasNextPage=false;
  72.         }
  73.         else {
  74.             hasNextPage=true;
  75.         }
  76.         if((currentPage-1)>0) {
  77.             hasPreviousPage=true;
  78.         }
  79.         else {
  80.             hasPreviousPage=false;
  81.         }
  82.         List<Country> list=this.getList(currentPage);
  83.         return list;
  84.     }
  85. }
  86. Action:
  87. public class PageListActionAction extends Action {
  88.     List<Country> list=new ArrayList<Country>();
  89.     Page pd;
  90.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  91.             HttpServletRequest request, HttpServletResponse response) {
  92.         String action;
  93.         int pageNum=1;
  94.         action=request.getParameter("action");
  95.        if(action==null||action.equals("")) {
  96.          try {
  97.              list=Dbutil.getBindTable();
  98.             }
  99.            catch(Exception e) {
  100.                 e.printStackTrace();
  101.             }
  102.             pd=new Page(list);
  103.             List<Country> country=pd.getList(pageNum);
  104.             request.getSession().setAttribute("list",country);
  105.             request.getSession().setAttribute("pa",pd);
  106.         }
  107.       else {
  108.           if(action=="nextPage"||action.equals("nextPage")) {
  109.              List<Country> country=pd.getNextPage();
  110.              request.getSession().setAttribute("list",country);
  111.              request.getSession().setAttribute("pa",pd);
  112.             }
  113.          if(action=="previousPage"||action.equals("previousPage")) {
  114.             List<Country> country=pd.getPreviousPage();
  115.             request.getSession().setAttribute("list",country);
  116.             request.getSession().setAttribute("pa",pd);
  117.          }
  118.      }
  119.       return mapping.findForward("success");
  120.     }
  121. }
  122. jsp:
  123. <html>
  124.   <head>
  125.      <title></title>
  126.  </head>
  127.   <body>
  128.       <table border="1">
  129.       <logic:present name="list">
  130.       <logic:iterate id="li" name="list" scope="session">
  131.       <logic:present name="li">
  132.         <tr>
  133.           <td><bean:write name="li" property="countryID"/></td>
  134.           <td><bean:write name="li" property="description"/></td>
  135.         </tr>
  136.         </logic:present>
  137.        </logic:iterate>
  138.        </logic:present>
  139.       </table>
  140.      
  141.    <logic:equal name="pa" property="hasNextPage" value="true">
      <html:link page="/pageListAction.do?action=nextPage">nextPage</html:link>
      </logic:equal>
     <logic:notEqual name="pa" property="hasPreviousPage" value="true">
       PreviousPage
      </logic:notEqual>
      <logic:equal name="pa" property="hasPreviousPage" value="true">
    <html:link page="/pageListAction.do?action=previousPage">PreviousPage</html:link>
    </logic:equal>
       <logic:notEqual name="pa" property="hasNextPage" value="true">
      nextPage
      </logic:notEqual>
  142.  共有数据总数<bean:write name="pa" property="totalRows"/>;
  143. 共分<bean:write name="pa" property="totalPages"/>页,当前是第
  144. <bean:write name="pa" property="currentPage"/>页
  145.   </body>
  146. </html>
  147. 查询类:
  148.     public static List<Country> getBindTable()
  149.     {
  150.         Country  country=null;
  151.         List<Country> list=new ArrayList<Country>();
  152.         try { 
  153.             Class.forName("org.gjt.mm.mysql.Driver"); 
  154.             Connection c = DriverManager.getConnection
  155. ("jdbc:mysql://192.168.0.11/database_name?user=root&password=0"); 
  156.             Statement s = c.createStatement(); 
  157.             ResultSet r =s.executeQuery("select ID,Country_ID,Description from 
  158. country_"); 
  159.             while(r.next()) {
  160.                 country=new Country();
  161.              country.setID(r.getInt("ID"));
  162.              country.setCountryID(r.getString("Country_ID"));
  163.              country.setDescription(r.getString("Description"));
  164.              list.add(country);
  165.             //System.out.println(r.getString("Country_ID")); 
  166.             } 
  167.             s.close(); // Also closes ResultSet 
  168.             } catch(Exception e) {e.printStackTrace();} 
  169.             return list;
  170.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值