3.分页辅助类
java 代码
- ViewPageHelper.java
- /**
- * 分页辅助类,用于减化Controller中的代码
- */
- public class ViewPageHelper {
- private static final int FIRST_PAGE_VALUE = 1;
- private static final int PREVIOUS_PAGE_VALUE = 2;
- private static final int NEXT_PAGE_VALUE = 3;
- private static final int LAST_PAGE_VALUE = 4;
- private static final int SPECIAL_PAGE_VALUE = 5;
- public static final String FIRST_PAGE = "FIRST_PAGE";
- public static final String PREVIOUS_PAGE = "PREVIOUS_PAGE";
- public static final String NEXT_PAGE = "NEXT_PAGE";
- public static final String LAST_PAGE = "LAST_PAGE";
- public static final String SPECIAL_PAGE = "SPECIAL_PAGE";
- /**分页动作参数名**/
- public static final String PAGE_ACTION = "page_action";
- /**分页对象属性名**/
- public static final String SESSION_PAGE = "session_page";
- /**页号参数名**/
- public static final String PAGE_NO = "page_no";
- private static Map actionMap = new HashMap();
- static {
- actionMap.put(FIRST_PAGE, new Integer(FIRST_PAGE_VALUE));
- actionMap.put(PREVIOUS_PAGE, new Integer(PREVIOUS_PAGE_VALUE));
- actionMap.put(NEXT_PAGE, new Integer(NEXT_PAGE_VALUE));
- actionMap.put(LAST_PAGE, new Integer(LAST_PAGE_VALUE));
- actionMap.put(SPECIAL_PAGE, new Integer(SPECIAL_PAGE_VALUE));
- }
- /**
- * 执行分页动作
- * @param page 分页对象
- * @param action 分页动作参数
- * @param pageIndex 页号
- */
- public static void doAction(ViewPage page, String action, int pageIndex) {
- int actionIndex = 0;
- if (page == null) {
- throw new NullPointerException("Page对象null");
- }
- if (action == null || "".equals(action)) {
- throw new IllegalArgumentException("无效的分页动作参数null");
- }
- action = action.toUpperCase();
- if (!actionMap.containsKey(action)) {
- throw new UnsupportedOperationException("不支持的分页动作参数:" + action);
- }
- Integer index = (Integer) actionMap.get(action);
- actionIndex = index.intValue();
- switch (actionIndex) {
- case FIRST_PAGE_VALUE:
- page.gotoFirstPage();
- break;
- case PREVIOUS_PAGE_VALUE:
- page.gotoPreviousPage();
- break;
- case NEXT_PAGE_VALUE:
- page.gotoNextPage();
- break;
- case LAST_PAGE_VALUE:
- page.gotoLastPage();
- break;
- case SPECIAL_PAGE_VALUE:
- page.gotoPage(pageIndex);
- }
- }
- public static void doAction(ViewPage page, String action){
- doAction(page, action, 1);
- }
- }
五、应用通用分页框架
1.继承AbstractViewPage类,实现queryPageList(int startRow, int endRow)和
queryTotalRows()方法。
protected int queryTotalRows() throws Exception
获得查询条件的总记录数
protected List queryPageList(int startRow, int rowCount)
用于查询指定范围的数据。startRow为开始记录号, rowCount为查询的记录数
queryPageList(0,20)为查询从第一条开始的20条记录。
使用Ibatis可以由queryPageList调用queryForList()方法。
1.继承AbstractViewPage类,实现queryPageList(int startRow, int endRow)和
queryTotalRows()方法。
protected int queryTotalRows() throws Exception
获得查询条件的总记录数
protected List queryPageList(int startRow, int rowCount)
用于查询指定范围的数据。startRow为开始记录号, rowCount为查询的记录数
queryPageList(0,20)为查询从第一条开始的20条记录。
使用Ibatis可以由queryPageList调用queryForList()方法。
java 代码
- /**
- * 用户信息分页内部类
- */
- class UserInfoPage extends AbstractViewPage{
- //------------------------------------------------
- //实现AbstractViewPage抽象类的抽象方法
- //------------------------------------------------
- /**
- * @see com.prs.application.ehld.web.mvc.AbstractViewPage#getPageDate(int, int)
- */
- protected List queryPageList(int startRow, int endRow) throws Exception {
- return sampleAction.getUserInfoList(startRow, endRow);
- }
- /**
- * @see com.prs.application.ehld.web.mvc.AbstractViewPage#getRows()
- */
- protected int queryTotalRows() throws Exception {
- return sampleAction.getUserCount();
- }
- }
- 3. 在Contrller中的实现
- public ModelAndView listUser(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- String pageAction =
- RequestUtils.getStringParameter(request,ViewPageHelper.PAGE_ACTION);
- Integer pageIndex =
- RequestUtils.getIntParameter(request,ViewPageHelper.PAGE_NO);
- //声明分页对象
- ViewPage userPage =
- (ViewPage) request.getSession().getAttribute(ViewPageHelper.SESSION_PAGE);
- //第一次请求
- if(pageAction == null || userPage == null){
- //构建一个新的分页对象
- userPage = new UserInfoPage();
- //设置分页大小
- userPage.setPageSize(2);
- }else{
- if(ViewPageHelper.SPECIAL_PAGE.equals(pageAction)){
- //如果页数为空,则默认为1
- if (pageIndex == null)
- pageIndex = new Integer(1);
- ViewPageHelper.doAction(userPage,pageAction,pageIndex.intValue());
- }else{
- ViewPageHelper.doAction(userPage,pageAction);
- }
- }
- //从分页对象中获得当前页数据
- List userInfoList = userPage.getPageData();
- ModelAndView mav = new ModelAndView(userInfoListView);
- mav.addObject(this.userInfoListKey,userInfoList);
- request.getSession().setAttribute(ViewPageHelper.SESSION_PAGE,userPage);
- return mav;
- }