创建完基本分页的工具类之后,然后就是到action里面的内容了,因为是连接到对方的数据库,所以有些代码省略.在Action中定义如下:
package org.lxh.action; public class OtherFavoMoreAction extends BaseAction { // 测试分页参数 private static final int DEFAULTLINESIZE = 20; private static final int DEFAULTCURPAGE = 1; // 每页多少条数据 private int pagesize; // 数据中总记录数 private int pagetoal; // 当前页 private int curpage; // 需要分多少页 private int totalsplitpage; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ConnectionService connectionService = (ConnectionService) this .getBean("connectionService"); Map<String, Object> map = new HashMap<String, Object>(); ShowControlBean showControl = getShowControl(request); try { DataCount = Integer.parseInt(request.getParameter("datacount")); String startIx = this.getParam(request, "startIx"); String maxAssets = this.getParam(request, "maxAssets"); // 每次要显示多少条记录 if (StringUtils.isEmpty(maxAssets)) pagesize = DEFAULTLINESIZE; else pagesize = Integer.parseInt(maxAssets); // 起始页 String startIx = this.getParam(request, "startIx"); if (StringUtils.isEmpty(startIx)) { curpage = DEFAULTCURPAGE; System.out .println("first---------------------------" + curpage); } else { curpage = Integer.parseInt(startIx); System.out.println("se---------------------------" + curpage); } /*----------------------------------------------------------------------------*/ // 当前页 List<User> vo = new ArrayList<User>(); List sqlList = userList.getMoreUser(DataCount); // 无数据时取数 for (int i = 0; i < sqlList.size(); i++) { User user= new User(); user= userService.getuser;
if (user== null) continue; else vo.add(user); map.clear(); } // 去掉空值的总记录数 pagetoal = vo.size(); totalsplitpage = PageUtil.spiltPage(pagetoal, pagesize); // 是否首页 request.setAttribute("isHomePage", PageUtil.ishomepage(curpage)); // 是否末页 request.setAttribute("isLastPage", PageUtil.islast(curpage, totalsplitpage)); // 是否有上一页 request.setAttribute("isFist", PageUtil.isfirst(curpage)); // 是否有下一页 request.setAttribute("isNext", PageUtil.isnext(curpage, totalsplitpage)); // 当前页 request.setAttribute("curpage", curpage); // 一共有多少数据 request.setAttribute("toal", pagetoal); // 要分多少页 request.setAttribute("pages", totalsplitpage); // 每条多少记录 request.setAttribute("linesize", pagesize); // 分页数据 request.setAttribute("vo", PageUtil.splitPageByListAssetVo(vo, pagesize, curpage)); request.setAttribute("DataCount", DataCount); return mapping.findForward("view"); } catch (Exception e) { e.printStackTrace(); return null; } } }
当页面点下一页的时候会跳转到这个action里面然后进行分页的处理,处理完之后返回一个List集合然后再次request到要显示的Jsp页面上去,虽然在Liferay中需要获取sp参数需要调用paramUtil方法取得数值,由于在项目中用的不是基于liferay ext的开发模式,只是利用了portlet的桥接,所以单纯的request.getParameter就可以取到需要的值.上面用到的 getParam是从request中进行处理后再返回的,防止了空指针的异常做出的优化.