设置分页查问PageUtil须要用到配置两个工具类:ServletUtil和StringUtil当然这两个工具类不仅仅服务于PageUtil
1.分页查问设置pageSize和pageCurrent两个参数时须要应用ServletRequest接口的getParameter办法

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues.以字符串模式返回申请参数的值,如果参数不存在,则返回null。申请参数是与申请一起发送的额定信息。对于HTTP servlet,参数蕴含在查问字符串或提交的表单数据中。只有当您确定参数只有一个值时,才应该应用此办法。如果参数可能有多个值,则应用getParameterValues。public String getParameter(String name);

2.应用getParameter办法须要获取request申请
我在ServletUtil类中应用getRequest()办法
ServletUtil类:

package com.cy.pj.common.util;import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class ServletUtil {    /* * 获取request * */ public static HttpServletRequest getRequest(){        return getRequestAttributes().getRequest();    }    /* * 获取response * */ public static HttpServletResponse getReponse(){        return getRequestAttributes().getResponse();    }    /* * 获取session * */ public static HttpSession getSession(){        return getRequest().getSession();    }    public static ServletRequestAttributes getRequestAttributes(){        RequestAttributes attributes=                RequestContextHolder.getRequestAttributes();        return (ServletRequestAttributes) attributes;    }}

2.1web依赖中的类RequestContextHolder的getRequestAttributes()办法获取申请参数并强转为ServletRequestAttributes类型

Return the RequestAttributes currently bound to the thread.Returns:the RequestAttributes currently bound to the thread, or null if none bound返回以后绑定到线程的RequestAttributes。返回:以后绑定到线程的RequestAttributes,如果没有绑定则返回null@Nullablepublic static RequestAttributes getRequestAttributes() {   RequestAttributes attributes = requestAttributesHolder.get();   if (attributes == null) {      attributes = inheritableRequestAttributesHolder.get();   }   return attributes;}

3.PageUtil类:

package com.cy.pj.common.util;import com.github.pagehelper.Page;import com.github.pagehelper.PageHelper;import javax.servlet.http.HttpServletRequest;public class PageUtil {    public static <T>Page<T> startPage(){        HttpServletRequest request=                ServletUtil.getRequest();        //页面大小(每页最多显示多少条记录) String pageSizeStr=request.getParameter("pageSize");        //以后页码值(要查第几页的数据包) String pageCurrentStr=request.getParameter("pageCurrent");        System.out.println("pageSize="+pageSizeStr);        System.out.println("pageCurrent="+pageCurrentStr);        /* * 在此地位调用pageHelper中的一个办法启动分页 * 在我的项目中去增加一个PageHelper依赖(后缀是starter)*/ Integer pageCurrent=                StringUtil.isEmpty(pageCurrentStr)?1:Integer.parseInt(pageCurrentStr);        Integer pageSize=                StringUtil.isEmpty(pageSizeStr)?10:Integer.parseInt(pageSizeStr);        return PageHelper.startPage( pageCurrent, pageSize);    }}

4.应用StringUtil类的isEmpty(String str)办法来判断获取的字符串类型的申请参数是否为null或者空字符,而后将申请参数通过Integer的parseInt办法转换为Integer类型

StringUtil类:

package com.cy.pj.common.util;public class StringUtil {    public static boolean isEmpty(String str){        return str==null||"".equals(str);    }}