strts2自定义拦截器使用小记

来源:互联网 发布:游戏加密软件 编辑:程序博客网 时间:2024/05/16 18:58
刚用Strus2的拦截器实现了一个自动设置分页参数,

在要启用分页方法的前置执行翻页参数获得:

@Before    public void methodBefore(){        //print("方法执行前");        // 获取翻页参数        Page rollPage = getPage();        ContextHolder.setPage(rollPage);    }

在方法后将分页参数清空:
@After    public void methodAfter(){        //print("方法执行后");        // 清理过期的翻页参数        ContextHolder.removePage();
拦截器配置:

<package name="jsonPackage" extends="json-default">        <interceptors>            <!-- 配置注解拦截器 -->            <interceptor name="annotationInterceptor"                          class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/>            <interceptor-stack name="pageStack">                                <interceptor-ref name="annotationInterceptor"/>                <interceptor-ref name="defaultStack"/>            </interceptor-stack>        </interceptors>    </package>
方法申明:

@Action(value = "queryUsers", interceptorRefs=@InterceptorRef("pageStack"),            results = {@Result(name = "success", type = "json", params = {"root", "dataGirdData"})})    public String queryUsers() {

设置好后发现翻页参数无法获得,总是使用默认值进行查询。

将拦载器去除,改用之前人工调用,一切正常;于是检查拦截器,最后发现在方法引用拦截器前加入

interceptorRefs={@InterceptorRef("defaultStack"),@InterceptorRef("pageStack")}

分页正常使用,于是想到是拦截器位置问题

    <package name="jsonPackage" extends="json-default">        <interceptors>            <!-- 配置注解拦截器 -->            <interceptor name="annotationInterceptor"                         class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/>            <interceptor-stack name="pageStack">                <interceptor-ref name="defaultStack"/>                <interceptor-ref name="annotationInterceptor"/>            </interceptor-stack>        </interceptors>    </package>


修改后翻页正常使用。
记录下来,免得下次再犯。



原创粉丝点击