標籤: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‘)">
* 在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">首頁 |</td>
<td width="10%" align="center">上一頁 |</td>
<%}else{ %>
<td width="8%" align="center"><u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘start‘)">首頁 |</a></u></td>
<td width="10%" align="center"><u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘prev‘)">上一頁 |</a></u></td>
<%} %>
<%if(pagebean.getLastPage()){ %>
<td width="10%" align="center">下一頁 |</td>
<td width="8%" align="center">末頁</td>
<%}else{ %>
<td width="10%" align="center"><u><a href="#" onClick="gotopage(‘system/elecUserAction_home.do‘,‘next‘)">下一頁 |</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();
}
});