如何进入Struts框架内部,从功能上讲必须能够处理客户端的Http请求.真实表示层框架的基本的要求.而SUN公司规定Servlet标准.有两种方式可以进行Http请求的处理.实现标准的Servlet接口和Filter接口.而Struts2为什么选择Filter??而Filter相对于Servlet接口的优势在哪?
从Servlet API中可以发现Servlet和Filter的区别:
a.实例何时被WEB服务器创建?
默认情况下第一次访问Servlet时,容器创建实例,初始化该对象.应用启动时,Filter对象被容器创建,执行init方法.Struts2容器需要初始化相关Bean.
b.Filter如果配置信息错误,服务器启动就会出错.抛出异常,可以较早发现程序错误.
|
StrutsPrepareAndExecuteFilter源码可知.所以从严格意义上的Struts2,实际上有两个不同的框架组成.传统Struts2的工作处理Http请求,然后委托XWork完成真正的逻辑处理.做到与Servlet API(即Web容器)分离.
|
<
bean
type
=
"com.opensymphony.xwork2.ObjectFactory"
name
=
"struts"
class
=
"org.apache.struts2.impl.StrutsObjectFactory"
/>
当Web容器启动时,就会加载配置文件,然后实例化该Bean.
|
Struts 2的拦截器实现相对简单.当请求到达Struts 2的ServletDispatcher时,Struts2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器. ![]() |
<interceptors>
<
interceptor
name
=
"alias"
class
=
"com.opensymphony.xwork2.interceptor.AliasInterceptor"
/>
<
interceptor
name
=
"autowiring"
class
=
"com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"
/>
<
interceptor
name
=
"chain"
class
=
"com.opensymphony.xwork2.interceptor.ChainingInterceptor"
/>
<
interceptor
name
=
"conversionError"
class
=
"org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"
/>
<
interceptor
name
=
"cookie"
class
=
"org.apache.struts2.interceptor.CookieInterceptor"
/>
<
interceptor
name
=
"cookieProvider"
class
=
"org.apache.struts2.interceptor.CookieProviderInterceptor"
/>
<
interceptor
name
=
"clearSession"
class
=
"org.apache.struts2.interceptor.ClearSessionInterceptor"
/>
<
interceptor
name
=
"createSession"
class
=
"org.apache.struts2.interceptor.CreateSessionInterceptor"
/>
<
interceptor
name
=
"debugging"
class
=
"org.apache.struts2.interceptor.debugging.DebuggingInterceptor"
/>
<
interceptor
name
=
"execAndWait"
class
=
"org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"
/>
<
interceptor
name
=
"exception"
class
=
"com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"
/>
<
interceptor
name
=
"fileUpload"
class
=
"org.apache.struts2.interceptor.FileUploadInterceptor"
/>
<
interceptor
name
=
"i18n"
class
=
"com.opensymphony.xwork2.interceptor.I18nInterceptor"
/>
<
interceptor
name
=
"logger"
class
=
"com.opensymphony.xwork2.interceptor.LoggingInterceptor"
/>
<
interceptor
name
=
"modelDriven"
class
=
"com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"
/>
<
interceptor
name
=
"scopedModelDriven"
class
=
"com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"
/>
<
interceptor
name
=
"params"
class
=
"com.opensymphony.xwork2.interceptor.ParametersInterceptor"
/>
<
interceptor
name
=
"actionMappingParams"
class
=
"org.apache.struts2.interceptor.ActionMappingParametersInteceptor"
/>
<
interceptor
name
=
"prepare"
class
=
"com.opensymphony.xwork2.interceptor.PrepareInterceptor"
/>
<
interceptor
name
=
"staticParams"
class
=
"com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"
/>
<
interceptor
name
=
"scope"
class
=
"org.apache.struts2.interceptor.ScopeInterceptor"
/>
<
interceptor
name
=
"servletConfig"
class
=
"org.apache.struts2.interceptor.ServletConfigInterceptor"
/>
<
interceptor
name
=
"timer"
class
=
"com.opensymphony.xwork2.interceptor.TimerInterceptor"
/>
<
interceptor
name
=
"token"
class
=
"org.apache.struts2.interceptor.TokenInterceptor"
/>
<
interceptor
name
=
"tokenSession"
class
=
"org.apache.struts2.interceptor.TokenSessionStoreInterceptor"
/>
<
interceptor
name
=
"validation"
class
=
"org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"
/>
<
interceptor
name
=
"workflow"
class
=
"com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"
/>
<
interceptor
name
=
"store"
class
=
"org.apache.struts2.interceptor.MessageStoreInterceptor"
/>
<
interceptor
name
=
"checkbox"
class
=
"org.apache.struts2.interceptor.CheckboxInterceptor"
/>
<
interceptor
name
=
"profiling"
class
=
"org.apache.struts2.interceptor.ProfilingActivationInterceptor"
/>
<
interceptor
name
=
"roles"
class
=
"org.apache.struts2.interceptor.RolesInterceptor"
/>
<
interceptor
name
=
"annotationWorkflow"
class
=
"com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"
/>
<
interceptor
name
=
"multiselect"
class
=
"org.apache.struts2.interceptor.MultiselectInterceptor"
/>
<
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
=
"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\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*
</
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-ref
name
=
"debugging"
/>
</
interceptor-stack
>
</
interceptors
>
|