Struts2拦截器defaultStack与paramsPrepareParamsStack

struts-default.xml中定义了一系列的拦截器和拦截器链

同时也定义了一个默认的拦截器defaultStack,一旦定义了默认的拦截器,

该拦截器就会对包中的所有的Action起作用,当然如果你的Action中显式地定义了拦截器,

默认拦截器就会失去作用,如果想不让他失去作用,

也必须显式地定义系统默认的拦截器。

  
    
</ interceptors >

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

< default-class-ref class ="com.opensymphony.xwork2.ActionSupport" />
</ package >


“defaultStack”和“paramsPrepareParamsStack”拦截器栈都提供了拦截器大部分功能

只是拦截顺序不一样。一般情况下会使用前者,但是某些涉及参数传递和取值的问题

例如实现了ModelDriven接口的某些时候,使用后者,就不用自己再额外编写拦截器类了

defaultStack
  
    
< interceptor-stack name ="defaultStack" >
< interceptor-ref name ="exception" />
< interceptor-ref name ="alias" />
< interceptor-ref name ="servletConfig" />
< interceptor-ref name ="i18n" />
< interceptor-ref name ="prepare" />
< interceptor-ref name ="chain" />
< interceptor-ref name ="debugging" />
< interceptor-ref name ="scopedModelDriven" />
< interceptor-ref name ="modelDriven" />
< interceptor-ref name ="fileUpload" />
< interceptor-ref name ="checkbox" />
< interceptor-ref name ="multiselect" />
< interceptor-ref name ="staticParams" />
< interceptor-ref name ="actionMappingParams" />
< interceptor-ref name ="params" >
< param name ="excludeParams" > dojo\..*,^struts\..* </ 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 >
paramsPrepareParamsStack
  
    
< interceptor-stack name ="paramsPrepareParamsStack" >
< interceptor-ref name ="exception" />
< interceptor-ref name ="alias" />
< interceptor-ref name ="i18n" />
< interceptor-ref name ="checkbox" />
< interceptor-ref name ="multiselect" />
< interceptor-ref name ="params" >
< param name ="excludeParams" > dojo\..*,^struts\..* </ param >
</ interceptor-ref >
< interceptor-ref name ="servletConfig" />
< interceptor-ref name ="prepare" />
< interceptor-ref name ="chain" />
< interceptor-ref name ="modelDriven" />
< interceptor-ref name ="fileUpload" />
< interceptor-ref name ="staticParams" />
< interceptor-ref name ="actionMappingParams" />
< interceptor-ref name ="params" >
< param name ="excludeParams" > dojo\..*,^struts\..* </ 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 >

defaultStack无法接收prepare()需要的数据,而应借助paramsPrepareParamsStack拦截器栈

使用prepare拦截器之前,应先调用params拦截器,prepare() 才能接收到表单数据

基于这个思路,可以通过各种手段将params拦截器放置在prepare拦截器之前即可

比如将defaultStack中的所有拦截器拷贝到struts.xml的我们自定义的myStack拦截器栈中

再按照paramsPrepareParamsStack拦截器栈中的params和prepare顺序修改二者位置即可 

你可能感兴趣的:(defaultStack)