Strus2 + pager-taglib implement pagination Summary

Source: Internet
Author: User

Seeing Kun GE's blog, he even encountered this problem. Let's make a summary here!

Currently, filters are used in the OA of Shang xuetang to set pagersize and offset values for threadlocal objects. in this way, you can dynamically add values without modifying the code logic of the manager layer. This is the idea of AOP (for Aspect-Oriented Programming.

I tested and transplanted the Code directly to use it in struts1.x without any problem, but an exception occurred in strus2: ognl. ognlexception: target is null for setproperty (null, "offset", [ljava. lang. string; @ c3dfb9). There are two methods to check the relevant information. The first method is to add modify webwork configuration in struts2. devmode = false; the second method is to write such code: httpservletrequest request = servletactioncontext. getrequest ();
If (request. getparameter ("pager. offset") = NULL ){
Offset = 0;
} Else {
Offset = integer. parseint (request. getparameter ("pager. offset "));
System. Out. println ("offset:" + offset );

After analysis, the first method is obviously to avoid the problem and is not solved at all. after the second method is added, the code is invasive, thus losing the significance of No-invasive AOP programming. I now provide the third method: Add the following sentence to the original action: Public pager = new pager (); Create a pager class, there are two variables pagersize and offset and the getter and setter methods are implemented. The reason is as follows: sturts2 supports pojo.

During the test, I found that the filter cannot be used to precisely locate the list display method and the page. Isn't that a lot of unnecessary repetition. can I precisely locate the display list method?

I first thought of JDK's dynamic proxy implementation, but this requires using the proxy class during client calls. later I thought of using spring's AOP programming. in this way, we can indeed implement the idea just now. later I thought of the interceptor using struts2. the interceptor can be nested with struts2. OK. The Interceptor is used. remove the original filter and use pagerinterceptor as the code below:

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"; @ overrideprotected string dointercept (actioninvocation invoc Ation) throws exception {string result = ""; try {// set pagesize and offset in the thread before calling the real method. httpservletrequest httprequest = servletactioncontext. getrequest (); systemcontext. setoffset (getoffset (httprequest); systemcontext. setpagesize (getpagesize (httprequest); // call the real method. result = invocation. invoke ();} catch (exception e) {e. printstacktrace (); throw new runtimeexception ("An error occurred while passing in the parameter during paging! ");} Finally {// whether or not the real method is called, The threadlocal object in the thread must be removed. 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) {// first, determine whether the request contains the pagesize parameter, If this parameter is set, the client is requesting to change the number of lines displayed on each page. 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) ;}// determines whether the current session has a pagesize value integer pagesize = (integer) httprequest. getsession (). getattribute (page_size_name); If (pagesize = NULL) {INTEGER pagesize = integer. parseint (servletactioncontext. getservletcontext (). getinitparameter ("pagesize"); // try is not used to intercept exceptions here, so that an exception is intercepted at the upper layer. the configurable number of records per page is expanded here. httprequest. getsession (). setattribute (page_size_name, pagesize); Return pagesize;} return pagesize ;}}

Configure the struts2 configuration file as follows:

               <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>

         <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>

Add the following configuration to Web. xml:

<context-param>   <param-name>page_size</param-name>   <param-value>5</param-value></context-param>

In addition, spring's AOP programming can be fully implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.