Struts2的Preparable接口

来源:互联网 发布:linux 修改 oracle sid 编辑:程序博客网 时间:2024/05/02 00:17

Struts2的Action在实现com.opensymphony.xwork2.Preparable接口后,就可以重写prepare()方法
此时在Action中,
prepare()方法的执行点是在:setXxx()execute()的执行之前


比如需求:在执行Action的方法前,接收前台隐藏域传过来的值,再根据该值执行相应逻辑
如前台传过来
ID,我们根据ID查找数据库对应的用户信息,再跳转到modify()中修改信息
但实际的运行过程中发现,通过
Debug断点调试得知prepare()方法接收到的ID值是零
即前台隐藏域中的ID值没有传过来,事实上
问题就出在默认的defaultStack拦截器栈
其实
defaultStack无法接收prepare()需要的数据,而应借助paramsPrepareParamsStack拦截器栈
事实上使用prepare拦截器之前,
应先调用params拦截器,prepare()才能接收到表单数据
基于这个思路,于是可以通过各种手段将
params拦截器放置在prepare拦截器之前即可
比如将
defaultStack中的所有拦截器拷贝struts.xml的我们自定义的myStack拦截器栈
再按照
paramsPrepareParamsStack拦截器栈中的paramsprepare顺序修改二者位置即可


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


Preparable 接口定义:
public interface Preparable {
    voidprepare() throws Exception;
}


prepare何时被执行:
prepare方法由PrepareInterceptor拦截器调用执行


com.opensymphony.xwork2.interceptor.PrepareInterceptor
    publicString 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 prefixedmethod", 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 prefixedmethod", 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 prefixedmethod", 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-stackname="basicStack">
               <interceptor-refname="exception"/>
               <interceptor-refname="servletConfig"/>
               <interceptor-refname="prepare"/>
               <interceptor-refname="checkbox"/>
               <interceptor-refname="multiselect"/>
               <interceptor-refname="actionMappingParams"/>
               <interceptor-ref name="params">
                   <paramname="excludeParams">dojo\..*,^struts\..*</param>
               </interceptor-ref>
               <interceptor-refname="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>


原创粉丝点击