27. SystemContext.removeOffset(); 28. SystemContext.removePagesize(); 29. } 30. return result; 31. } 32. 33. private int getOffset(HttpServletRequest request) { 34. int offset = 0; 35. try { 36. offset = Integer.parseInt(request.getParameter("pager.offset")); 37. } catch (Exception ignore) { 38. } 39. return offset; 40. } 41. private int getPagesize(HttpServletRequest httpRequest) { 42. // 首先判断request中是否有pagesize参数,如果有这个参数,证明客户端正在请求改变每页显示的行数 43. String psvalue = httpRequest.getParameter(PAGE_SIZE_NAME); 44. if (psvalue != null && !psvalue.trim().equals("")) { 45. Integer ps = 0; 46. try { 47. ps = Integer.parseInt(psvalue); 48. } catch (Exception e) { 49. } 50. if (ps != 0) { 51. httpRequest.getSession().setAttribute(PAGE_SIZE_NAME, ps); 52. } 53. } 54. // 判断当前session中是否有pagesize的值 55. Integer pagesize = (Integer) httpRequest.getSession().getAttribute(PAGE_SIZE_NAME); 56. if (pagesize == null) { 57. Integer pageSize= Integer.parseInt(ServletActionContext.getServletContext().getInitParameter("pageSize"));//这里没有用try拦截异常,以为在它的上层拦截了异常.此处进行了每页记录数可配置的扩展功能. 58. httpRequest.getSession().setAttribute(PAGE_SIZE_NAME, pageSize); 59. return pageSize; 60. } 61. return pagesize; 62. } 63. } package com.ssh.utile;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class PagerInterceptor extends MethodFilterInterceptor { public static final String PAGE_SIZE_NAME = "ps"; @Override protected String doIntercept(ActionInvocation invocation) throws Exception { String result=""; try { //在调用真正的方法之前在线程内设置pagesize和offset. HttpServletRequest httpRequest = ServletActionContext.getRequest(); SystemContext.setOffset(getOffset(httpRequest)); SystemContext.setPagesize(getPagesize(httpRequest)); //调用真正的方法. result=invocation.invoke(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("分页时传入参数出错!"); }finally { //不管是否调用真正的方法,都要移除线程内的ThreadLocal对象. SystemContext.removeOffset(); SystemContext.removePagesize(); } return result; }
private int getOffset(HttpServletRequest request) { int offset = 0; try { offset = Integer.parseInt(request.getParameter("pager.offset")); } catch (Exception ignore) { } return offset; } private int getPagesize(HttpServletRequest httpRequest) { // 首先判断request中是否有pagesize参数,如果有这个参数,证明客户端正在请求改变每页显示的行数 String psvalue = httpRequest.getParameter(PAGE_SIZE_NAME); if (psvalue != null && !psvalue.trim().equals("")) { Integer ps = 0; try { ps = Integer.parseInt(psvalue); } catch (Exception e) { } if (ps != 0) { httpRequest.getSession().setAttribute(PAGE_SIZE_NAME, ps); } } // 判断当前session中是否有pagesize的值 Integer pagesize = (Integer) httpRequest.getSession().getAttribute(PAGE_SIZE_NAME); if (pagesize == null) { Integer pageSize= Integer.parseInt(ServletActionContext.getServletContext().getInitParameter("pageSize"));//这里没有用try拦截异常,以为在它的上层拦截了异常.此处进行了每页记录数可配置的扩展功能. httpRequest.getSession().setAttribute(PAGE_SIZE_NAME, pageSize); return pageSize; } return pagesize; } } 在Struts2的配置文件中配置如下:
[html] view plaincopyprint? 01. <interceptors> 02. <interceptor name="pager" class="com.ssh.utile.PagerInterceptor" /> 03. <interceptor-stack name="myInterceptor"> 04. <interceptor-ref name="pager"> 05. <!--<param name="includeMethods">queryUser</param>--> 06. </interceptor-ref> 07. <interceptor-ref name="defaultStack"></interceptor-ref> 08. </interceptor-stack> 09.</interceptors> <interceptors> <interceptor name="pager" class="com.ssh.utile.PagerInterceptor" /> <interceptor-stack name="myInterceptor"> <interceptor-ref name="pager"> <!--<param name="includeMethods">queryUser</param>--> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors>
[html] view plaincopyprint? 01. <action name="user" class="userAction" > 02. <result name="showQuery">/showQuery.jsp</result> 03. <result name="error">/error.jsp</result> 04. <result name="showUpdate">/update.jsp</result> 05. <result name="success">/pub_add_success.jsp</result> 06. <interceptor-ref name="myInterceptor"></interceptor-ref> 07.</action> <action name="user" class="userAction" > <result name="showQuery">/showQuery.jsp</result> <result name="error">/error.jsp</result> <result name="showUpdate">/update.jsp</result> <result name="success">/pub_add_success.jsp</result> <interceptor-ref name="myInterceptor"></interceptor-ref> </action> 在web.xml中加入下面的配置:运动会加油稿
[html] view plaincopyprint? 01.<context-param> 02. <param-name>page_size</param-name> 03. <param-value>5</param-value> 04.</context-param> <context-param> <param-name>page_size</param-name> <param-value>5</param-value> </context-param> 再加一句,使用Spring的AOP编程也是完全可以实现的. 上一页 [1] [2]
|