概述
关于 WebWork 中Action 的prepare方法调用多次的问题
1. WebWork 的Action 实现 com.opensymphony.xwork.Preparable 接口 的 prepare() 方法时,在程序实际运行中往往这个方法被莫名其妙的调用多次。主要原因在存,调用此方法有两种途径:
一种就是直接实现Preparable接口,另一种就是在xwork的配置文件配置拦截器,其实WebWork的基础配置文件里已经配置好了,很多拦截器,其中也包括 调用preare() 方法拦截器,而我们,在不清楚WebWork基础配置文件的情况下,却配置引用这个拦截器。所以才会调用两次多次。
Xwork的基础配置文件如下:
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<package name="webwork-default">
<result-types>
<result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
<result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
<result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/>
<result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/>
<result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/>
<result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="com.opensymphony.webwork.dispatcher.HttpHeaderResult"/>
<result-type name="stream" class="com.opensymphony.webwork.dispatcher.StreamResult"/>
</result-types>
<interceptors>
<interceptor name="timer" class="com.opensymphony.xwork.interceptor.TimerInterceptor"/>
<interceptor name="logger" class="com.opensymphony.xwork.interceptor.LoggingInterceptor"/>
<interceptor name="chain" class="com.opensymphony.xwork.interceptor.ChainingInterceptor"/>
<interceptor name="static-params" class="com.opensymphony.xwork.interceptor.StaticParametersInterceptor"/>
<interceptor name="params" class="com.opensymphony.xwork.interceptor.ParametersInterceptor"/>
<interceptor name="model-driven" class="com.opensymphony.xwork.interceptor.ModelDrivenInterceptor"/>
<interceptor name="component" class="com.opensymphony.xwork.interceptor.component.ComponentInterceptor"/>
<interceptor name="token" class="com.opensymphony.webwork.interceptor.TokenInterceptor"/>
<interceptor name="token-session" class="com.opensymphony.webwork.interceptor.TokenSessionStoreInterceptor"/>
<interceptor name="validation" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>
<interceptor name="workflow" class="com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor"/>
<interceptor name="servlet-config" class="com.opensymphony.webwork.interceptor.ServletConfigInterceptor"/>
<interceptor name="prepare" class="com.opensymphony.xwork.interceptor.PrepareInterceptor"/>
<interceptor name="conversionError" class="com.opensymphony.webwork.interceptor.WebWorkConversionErrorInterceptor"/>
<interceptor name="fileUpload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"/>
<interceptor name="execAndWait" class="com.opensymphony.webwork.interceptor.ExecuteAndWaitInterceptor"/>
<!-- Basic stack -->
<interceptor-stack name="defaultStack">
<interceptor-ref name="servlet-config"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="static-params"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
</interceptor-stack>
<!-- Sample validation and workflow stack -->
<interceptor-stack name="validationWorkflowStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
<!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Sample Inversion of Control stack -->
<interceptor-stack name="componentStack">
<interceptor-ref name="component"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Sample model-driven stack -->
<interceptor-stack name="modelDrivenStack">
<interceptor-ref name="model-driven"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Sample action chaining stack -->
<interceptor-stack name="chainStack">
<interceptor-ref name="chain"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Sample execute and wait stack.
Note: execAndWait should always be the *last* interceptor. -->
<interceptor-stack name="executeAndWaitStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="execAndWait"/>
</interceptor-stack>
<!-- A complete stack with all the common interceptors in place.
Generally, this stack should be the one you use, though it
may process additional stuff you don't need, which could
lead to some performance problems. Also, the ordering can be
switched around (ex: if you wish to have your components
before prepare() is called, you'd need to move the component
interceptor up -->
<interceptor-stack name="completeStack">
<interceptor-ref name="prepare"/>
<interceptor-ref name="servlet-config"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="model-driven"/>
<interceptor-ref name="component"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="static-params"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
</interceptors>
</package>
</xwork>
解决方法就是:
1.不实现Preparable接口,但有 prepare() 方法。
Public class myAction extends BaseAction implements Preparable{
Public void prepare() throws Exception {
}
}
2.实现Preparable接口,但配置文件中不引用任何 调用prepare方法的拦截器。
配置实现:
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="params"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
<action>
以前是小弟的个人观点,不当之处还请多多赐教。
转载于:https://blog.51cto.com/jakemanse/294222
最后
以上就是忧心汉堡为你收集整理的关于 WebWork 中Action 的prepare方法调用多次的问题的全部内容,希望文章能够帮你解决关于 WebWork 中Action 的prepare方法调用多次的问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复