Struts2拦截器的使用 2

上述说明的拦截器在默认的Struts2应用中,根据惯例配置了若干个拦截器堆栈,详细情参看struts-default.xml

其中有一个拦截器堆栈比较特殊,他会应用在默认的每一个Action上。

<interceptor-stack name="defaultStack">

    <interceptor-ref name="exception"/>

    <interceptor-ref name="alias"/>

    <interceptor-ref name="servletConfig"/>

    <interceptor-ref name="prepare"/>

    <interceptor-ref name="i18n"/>

    <interceptor-ref name="chain"/>

    <interceptor-ref name="debugging"/>

    <interceptor-ref name="profiling"/>

    <interceptor-ref name="scopedModelDriven"/>

    <interceptor-ref name="modelDriven"/>

    <interceptor-ref name="fileUpload"/>

    <interceptor-ref name="checkbox"/>

    <interceptor-ref name="staticParams"/>

    <interceptor-ref name="params">

      <param name="excludeParams">dojo\..*</param>

    </interceptor-ref>

    <interceptor-ref name="conversionError"/>

    <interceptor-ref name="validation">

        <param name="excludeMethods">input,back,cancel,browse</param>

    </interceptor-ref>

    <interceptor-ref name="workflow">

        <param name="excludeMethods">input,back,cancel,browse</param>

    </interceptor-ref>

</interceptor-stack>

 

每一个拦截器都可以配置参数,有两种方式配置参数,一是针对每一个拦截器定义参数,二是针对一个拦截器堆栈
统一定义所有的参数,例如:

<interceptor-ref name="validation">

  <param name="excludeMethods">myValidationExcudeMethod</param>

</interceptor-ref>

<interceptor-ref name="workflow">

  <param name="excludeMethods">myWorkflowExcludeMethod</param>

</interceptor-ref>


或者

<interceptor-ref name="defaultStack">

    <param name="validation.excludeMethods">myValidationExcludeMethod</param>

    <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param>

</interceptor-ref>

 

每一个拦截器都有两个默认的参数:

excludeMethods - 过滤掉不使用拦截器的方法和

includeMethods ?C 使用拦截器的方法。

 

需要说明的几点:

1 拦截器执行的顺序按照定义的顺序执行,例如:

<interceptor-stack name="xaStack">

  <interceptor-ref name="thisWillRunFirstInterceptor"/>

  <interceptor-ref name="thisWillRunNextInterceptor"/>

  <interceptor-ref name="followedByThisInterceptor"/>

  <interceptor-ref name="thisWillRunLastInterceptor"/>

</interceptor-stack>

的执行顺序为:

thisWillRunFirstInterceptor

  thisWillRunNextInterceptor

    followedByThisInterceptor

      thisWillRunLastInterceptor

        MyAction1

        MyAction2 (chain)

        MyPreResultListener

        MyResult (result)

      thisWillRunLastInterceptor

    followedByThisInterceptor

  thisWillRunNextInterceptor

thisWillRunFirstInterceptor

 

2 使用默认拦截器配置每个Action都需要的拦截器堆栈,例如:

<action name="login"  class="tutorial.Login">

     <interceptor-ref name="timer"/>

     <interceptor-ref name="logger"/>

     <interceptor-ref name="default-stack"/>

 

     <result name="input">login.jsp</result>

     <result type="redirect-action">/secure/home</result>

</action>

可以按照如下的方式定义:

<interceptors>

     <interceptor-stack name="myStack">

        <interceptor-ref name="timer"/>

        <interceptor-ref name="logger"/>

        <interceptor-ref name="default-stack"/>

     </interceptor-stack>

</interceptors>

 

<default-interceptor-ref name="myStack"/>

 

<action name="login"  class="tutorial.Login">

       <result name="input">login.jsp</result>

       <result type="redirect-action">/secure/home</result>

</action>

 

3 如何访问HttpServletRequestHttpServletResponse或者HttpSession

有两种方法可以达到效果,使用ActionContext

Map attibutes = ActionContext.getContext().getSession();

或者实现相应的接口:

HttpSession            SessionAware

HttpServletRequest     ServletRequestAware

HttpServletResponse    ServletResponseAware

你可能感兴趣的:(xml,jsp,workflow,struts,dojo)