struts2.0一些重要的拦截器总结

标签:, , ,

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

首先,第一个是exception    用法如下,如果不用这个功能可以去掉

         <action name="test">
             
<interceptor-ref name="exception"/>
             
<interceptor-ref name="basicStack"/>
             
<exception-mapping exception="com.acme.CustomException" result="custom_error"/>
             
<result name="custom_error">custom_error.ftl</result>
         </action>

第二个,是取别名的功能,也可以基本不用

 <action name="someAction" class="com.examples.SomeAction">
     
<!– The value for the foo parameter will be applied as if it were named bar –>
     
<param name="aliases">#{ ’foo’ : ’bar’ }</param>

     
<interceptor-ref name="alias"/>
     
<interceptor-ref name="basicStack"/>
     
<result name="success">good_result.ftl</result>
 
</action>

第三个,看一下代码就明白了,这个有时还是能用到的,暂且保留好了(我们可以用 ServletActionContext代替)

        if (action instanceof ServletRequestAware) {
            HttpServletRequest request 
= (HttpServletRequest) context.get(HTTP_REQUEST);
            ((ServletRequestAware) action).setServletRequest(request);
        }


        
if (action instanceof ServletResponseAware) {
            HttpServletResponse response 
= (HttpServletResponse) context.get(HTTP_RESPONSE);
            ((ServletResponseAware) action).setServletResponse(response);
        }


        
if (action instanceof ParameterAware) {
            ((ParameterAware) action).setParameters(context.getParameters());
        }


        
if (action instanceof RequestAware) {
            ((RequestAware) action).setRequest((Map) context.get(
"request"));
        }


        
if (action instanceof SessionAware) {
            ((SessionAware) action).setSession(context.getSession());
        }


        
if (action instanceof ApplicationAware) {
            ((ApplicationAware) action).setApplication(context.getApplication());
        }

第四个 prepare  这个拦截器的控制粒度在方法级别,常用在一些方法的执行必须基于某某方法先执行

    public String doIntercept(ActionInvocation invocation) throws Exception {
        Object action 
= invocation.getAction();

        
if (action instanceof Preparable) {
            
try {
                PrefixMethodInvocationUtil.invokePrefixMethod(invocation,
                    
new String[] { PREPARE_PREFIX, ALT_PREPARE_PREFIX });
            }

            
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);
            }

            
if (alwaysInvokePrepare) {
                ((Preparable) action).prepare(); 先执行
            }

        }


        
return invocation.invoke(); 后执行
    }

另外这个拦截器要设置

  • excludeMethods - methods name to be excluded
  • includeMethods - methods name to be included

这2个属性 因为它是继承自 MethodFilterInterceptor

第五个,就不用说了,非常重要  foo.action?request_locale=en_US 

如果看不惯request_locale,可以设置掉 提供了
    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }







留言