1.Limit 默認的情況下eXtremeTable取得所有的結(jié)果集然后處理Beans集合,,這樣的好處是 你可以隨意進行排序,、過濾和分頁操作,。 你只需要組裝Beans集合并讓eXtremeTable知道如何 然而在查詢結(jié)果較大,,可以考慮使用數(shù)據(jù)庫端分頁的方案,。此時,,eXtremeTable 會使用Limit對象向Action/Controller傳遞PageNo,PageSize,OrderBy等分頁信息,。而服務端將向EC返回總記錄數(shù)和當前頁的內(nèi)容。
2.創(chuàng)建Limit
首先你需要通過LimitFactory得到一個Limit實例:
代碼
- Context context = new HttpServletRequestContext(request);
- LimitFactory limitFactory = new TableLimitFactory(context);
- Limit limit = new TableLimit(limitFactory);
Limit對象定義了limit結(jié)果集的所有方法,。 TableLimitFactory具有另外一個構(gòu)造函數(shù),,如果JSP頁面使用了兩個(以上)eXtremeTable的話你可以利用tableId分別傳輸totalRows,如果默認tableId將為所有ec,。
Context context = new HttpServletRequestContext(request); LimitFactory limitFactory = new TableLimitFactory(context, tableId); Limit limit = new TableLimit(limitFactory); 設置行屬性:
limit.setRowAttributes(totalRows, DEFAULT_ROWS_DISPLAYED);
3. 前臺jsp頁面中ec標薟寫法:
代碼
- <ec:table
- items="userlist"
- var="u"
- action="/pageAction.do"
- title="后臺用戶管理——管理用戶"
- retrieveRowsCallback="limit"
- width="80%"
- rowsDisplayed="3"
- form=""
- >
- <ec:row>
- ...
- ...
- </ec:row>
- </ec:table>
使用callback需要做的唯一事情是傳輸集合到request,,同時傳輸totalRows屬性,。 totalRows表示總行數(shù),,
代碼
- request.setAttribute("userlist", userlist);
- request.setAttribute("totalRows", new Integer(總記錄數(shù)));
4.實例: 本實例是ec結(jié)合Struts和Hibernate進行分頁.
PageAction中的部分代碼:
代碼
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- UserDAO userDAO = (UserDAO) getBean("userDAO");
- Limit limit = ExtremeTablePage.getLimit(request,defaultPageSize);
- PageBean page = new PageBean();
- page.setPageSize(limit.getCurrentRowsDisplayed());
- page.setPage(limit.getPage());
- List list = userDAO.findAllUser(page);
- request.setAttribute("userlist", list);
- request.setAttribute("totalRows", new Integer(page.getCount()));
-
- return mapping.findForward("userlist");
-
- }
Limit對象的實例化在ExtremeTablePage中
代碼
- static public Limit getLimit(HttpServletRequest request,int defaultPageSize) {
- Context context = new HttpServletRequestContext(request);
- LimitFactory limitFactory = new TableLimitFactory(context);
- TableLimit limit = new TableLimit(limitFactory);
- limit.setRowAttributes(Integer.MAX_VALUE, defaultPageSize);
- return limit;
- }
PageBean類部分代碼:
代碼
- public class PageBean {
- private int count = 0;
- private int pageSize = 10;
- private int pageCount = 0;
- private int page = 1;
- .
- .
- .
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- if (pageSize != 0) {
- pageCount = count / pageSize;
- if (count % pageSize != 0) {
- pageCount++;
- }
- }
- this.count = count;
- }
-
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- public int getPageCount() {
- return pageCount;
- }
- public void setPageCount(int pageCount) {
- this.pageCount = pageCount;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- .
- .
- .
- }
dao層使用PageBean對象進行查詢,。
|