博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2 Preparable接口
阅读量:6284 次
发布时间:2019-06-22

本文共 2540 字,大约阅读时间需要 8 分钟。

 

使用场景:

如果action针对每次请求都要执行一些相同的业务逻辑, 那么可以实现Preparable接口,将预处理业务逻辑写在prepare()方法里

 

Preparable 接口定义:

public interface Preparable {
    void prepare() throws Exception;
}

 

prepare何时被执行:

prepare方法由PrepareInterceptor拦截器调用执行

com.opensymphony.xwork2.interceptor.PrepareInterceptor
    public String doIntercept(ActionInvocation invocation) throws Exception {
        Object action = invocation.getAction();

       if (action instanceof Preparable) {

            try {
                String[] prefixes;
                if (firstCallPrepareDo) {
                    prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
                } else {
                    prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
                }
                PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
            }
            catch (InvocationTargetException e) {
                // just in case there's an exception while doing reflection,
                // we still want prepare() to be able to get called.
                LOG.warn("an exception occured while trying to execute prefixed method", e);
            }
            catch (IllegalAccessException e) {
                // just in case there's an exception while doing reflection,
                // we still want prepare() to be able to get called.
                LOG.warn("an exception occured while trying to execute prefixed method", e);
            } catch (Exception e) {
                // just in case there's an exception while doing reflection,
                // we still want prepare() to be able to get called.
                LOG.warn("an exception occured while trying to execute prefixed method", e);
            }

            // 必须执行或初始化的一些业务操作 action prepare()方法

            if (alwaysInvokePrepare) {
                ((Preparable) action).prepare();
            }
        }

       return invocation.invoke();

    }

 

使用方法:

使用basicStack拦截器栈

<action name="list" class="org.apache.struts2.showcase.action.SkillAction" method="list">
 <result>/empmanager/listSkills.jsp</result>
 <interceptor-ref name="basicStack" />    <!-- 使用basicStack拦截器栈, 该拦截器栈在 struts-default.xml 已配置, 包括了prepare -->
</action>

(注: struts-default.xml  里定义的 basicStack拦截器栈

    <!-- Basic stack -->

            <interceptor-stack name="basicStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="multiselect"/>
                <interceptor-ref name="actionMappingParams"/>
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
            </interceptor-stack>

)

或者直接指定prepare拦截器

<action name="list" class="org.apache.struts2.showcase.action.SkillAction" method="list">

 <result>/empmanager/listSkills.jsp</result>
 <interceptor-ref name="prepare" />   <!-- 直接指定prepare拦截器 -->
</action>

 

 

转载于:https://www.cnblogs.com/mount/archive/2011/11/08/2241046.html

你可能感兴趣的文章
Android 图片的缩略图
查看>>
一个iOS 框架介绍:MKNetworkKit
查看>>
【leetcode】Word Ladder II(hard)★ 图 回头看
查看>>
Eclipse断点调试
查看>>
WP8.1学习系列(第四章)——交互UX之导航模式
查看>>
Android tabhost下的activity怎样获取传来的值
查看>>
JVM内存配置详解(转)
查看>>
Eclipse代码格式化
查看>>
嵌套结构使用:struc1-struc2-XXX
查看>>
XMPP协议的原理介绍
查看>>
2015第7周日
查看>>
Shell解释器(学习笔记四)
查看>>
html在线美化网站
查看>>
开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
查看>>
c#如何使用正则表达式ZZ
查看>>
关于VC中的附加进程调试
查看>>
MongoDB-固定集合 capped collection 操作 介绍
查看>>
npoi实现 从固定的行读取数据作为表头并返回datable
查看>>
【Hibernate学习】 ——ORM(三)
查看>>
概率dp入门
查看>>