裝置資源管理系統-分頁

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

裝置資源管理系統-分頁

  • 分頁設計

  userindex.jsp

userList.jsp

  Ajax架構

      要求:

        1、需要2個頁面(XXXindex.jsp,XXXlist.jsp)

        2、一個jsp頁面中使用2個form對象,分別為Form1和Form2。

        3、另一個jsp的頁面中第一個jsp頁面的form2的內容。

      原理:

      提交過程,實質是提交Form1的參數,使用Fom1的參數,查詢後台資料,返回結果,將結果在XXXlist.jsp上顯示,用XXXlist.jsp的內容替換XXXIndex.jsp中Form2的內容

  • 部分代碼

1、匯入2個java檔案,分別是PageBean和PageInfo

   PageBean中的代碼:

    private int pageNo;          //當前第幾頁

    private boolean firstPage;   //當前頁是否是第一頁

    private boolean lastPage;    //當前頁是否是最後一頁

    private int sumPage;         //存放總頁數

    private int pageSize ;       //當前頁顯示幾條記錄

    private int totalResult ;    //存放總記錄數

2、匯入1個js檔案,是page.js,放入到script檔案夾下

3、修改userIndx.jsp

   * 匯入需要的js檔案

     <script language="javascript" src="${pageContext.request.contextPath }/script/function.js"></script>

  <script language="javascript" src="${pageContext.request.contextPath }/script/pub.js"></script>

  <script language="javascript" src="${pageContext.request.contextPath }/script/validate.js"></script>

  <script language="javascript" src="${pageContext.request.contextPath }/script/page.js"></script>

   * 在form1中,添加3個隱藏欄位,用於傳遞參數

   * 修改查詢按鈕的onclick事件

     <input style="font-size:12px; color:black; height=20;width=80" id="BT_Add" type="button" value="查詢" name="BT_find"

       onclick="gotoquery(‘system/elecUserAction_home.do‘)">&nbsp;&nbsp;

   * 在Form2中添加分頁的操作

      <!-- ly add start-->

     <tr>

           <td width="100%" height="1"  >

             <table border="0" width="100%" cellspacing="0" cellpadding="0">

             <%PageBean pagebean=(PageBean)request.getAttribute("page");%>

               <tr>

                 <td width="15%" align="left">總記錄數:<%=pagebean.getTotalResult() %>條</td>

                 <td width="14%" align="right"></td>     

                 <%if(pagebean.getFirstPage()){ %>          

                 <td width="8%" align="center">首頁&nbsp;&nbsp;|</td>

                 <td width="10%" align="center">上一頁&nbsp;&nbsp;&nbsp;|</td>

                 <%}else{ %>

                 <td width="8%" align="center"><u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘start‘)">首頁&nbsp;&nbsp;|</a></u></td>

                 <td width="10%" align="center"><u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘prev‘)">上一頁&nbsp;&nbsp;&nbsp;|</a></u></td>

                 <%} %>

                 <%if(pagebean.getLastPage()){ %>

        <td width="10%" align="center">下一頁&nbsp;&nbsp;&nbsp;|</td>

                 <td width="8%" align="center">末頁</td>

                 <%}else{ %>

                 <td width="10%" align="center"><u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘next‘)">下一頁&nbsp;&nbsp;&nbsp;|</a></u></td>

                 <td width="8%" align="center"><u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘end‘)">末頁</a></u></td>

                 <%} %>

                 <td width="6%" align="center">第<%=pagebean.getPageNo() %>頁</td>

                 <td width="6%" align="center">共<%=pagebean.getSumPage() %>頁</td>

                 <td width="21%" align="right">至第<input size="1" type="text" name="goPage" >頁

   

   

   

                 <u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘go‘)">確定</a></u></td>

                

                 <td><input type="hidden" name="pageNO" value="<%=pagebean.getPageNo()%>" ></td>

                 <td><input type="hidden" name="prevpageNO" value="<%=(pagebean.getPageNo()-1)%>"></td>

                 <td><input type="hidden" name="nextpageNO" value="<%=(pagebean.getPageNo()+1)%>"></td>

                 <td><input type="hidden" name="sumPage" value="<%=pagebean.getSumPage() %>" ></td>

                 <td><input type="hidden" name="pageSize" value="" ></td>

               </tr>

             </table>      

           </td>

         </tr>

    <!-- ly add  end-->  

4、提取userIndex.jsp中Form2的內容,建立userList.jsp

5、在struts.xml的設定檔中,添加:

    <!-- 2011-12-12,添加分頁的頁面轉寄 -->

   <result name="list">

    /WEB-INF/page/system/userList.jsp

   </result>

6、在ElecUserAction.java中修改

   //2011-12-12 添加分頁,傳遞request對象

   List<ElecUserForm> list = elecUserService.findElecUserListByCondition(elecUserForm,request);

   request.setAttribute("userList", list);

   String initflag = request.getParameter("initflag");

   if(initflag!=null && initflag.equals("1")){

     return "list";

   }

7、在ElecUserServiceImpl.java中添加

   //2011-12-12,添加分頁操作

   //List<ElecUser> list = elecUserDao.findCollectionByConditionNoPage(hqlWhere, params, orderby);

   PageInfo pageInfo = new PageInfo(request);

   操作邏輯:

      使用currentPageNo屬性,表示當前是第幾頁,預設是第一頁

      使用pageSize屬性,表示當前頁顯示的記錄數

      使用req屬性,存放頁面傳遞的request對象

   List<ElecUser> list = elecUserDao.findCollectionByConditionWithPage(hqlWhere, params, orderby, pageInfo);

   request.setAttribute("page", pageInfo.getPageBean());

8、在CommonDaoImpl.java中修改

   List<T> list = (List<T>)this.getHibernateTemplate().execute(new HibernateCallback(){

   public Object doInHibernate(Session session)

     throws HibernateException, SQLException {

    Query query = session.createQuery(finalHql);

    setParams(query,params);

    pageInfo.setTotalResult(query.list().size());

    操作邏輯:

       使用totalResult屬性,存放查詢列表的總的記錄數

       使用totalPage屬性,存放總頁數(使用總記錄數對每頁顯示的記錄數進行計算求得)

    query.setFirstResult(pageInfo.getBeginResult());

    操作邏輯:

       使用beginResult屬性,表示結果清單從第幾條開始顯示,也就是說,每頁的第一條顯示的資料

       執行個體化PageBean,添加屬性

      

    query.setMaxResults(pageInfo.getPageSize());

    return query.list();

   }

  });

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.