关于 WebWork 中Action 的prepare方法调用多次的问题
1. WebWork 的Action 实现 com.opensymphony.xwork.Preparable 接口 的 prepare() 方法时,在程序实际运行中往往这个方法被莫名其妙的调用多次。主要原因在存,调用此方法有两种途径:
一种就是直接实现Preparable接口,另一种就是在xwork的配置文件配置拦截器,其实WebWork的基础配置文件里已经配置好了,很多拦截器,其中也包括 调用preare() 方法拦截器,而我们,在不清楚WebWork基础配置文件的情况下,却配置引用这个拦截器。所以才会调用两次多次。
Xwork的基础配置文件如下:
"-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
"webwork-default">
"dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
"redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
"velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
"chain" class="com.opensymphony.xwork.ActionChainResult"/>
"xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/>
"jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/>
"freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/>
"httpheader" class="com.opensymphony.webwork.dispatcher.HttpHeaderResult"/>
"stream" class="com.opensymphony.webwork.dispatcher.StreamResult"/>
"timer" class="com.opensymphony.xwork.interceptor.TimerInterceptor"/>
"logger" class="com.opensymphony.xwork.interceptor.LoggingInterceptor"/>
"chain" class="com.opensymphony.xwork.interceptor.ChainingInterceptor"/>
"static-params" class="com.opensymphony.xwork.interceptor.StaticParametersInterceptor"/>
"params" class="com.opensymphony.xwork.interceptor.ParametersInterceptor"/>
"model-driven" class="com.opensymphony.xwork.interceptor.ModelDrivenInterceptor"/>
"component" class="com.opensymphony.xwork.interceptor.component.ComponentInterceptor"/>
"token" class="com.opensymphony.webwork.interceptor.TokenInterceptor"/>
"token-session" class="com.opensymphony.webwork.interceptor.TokenSessionStoreInterceptor"/>
"validation" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>
"workflow" class="com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor"/>
"servlet-config" class="com.opensymphony.webwork.interceptor.ServletConfigInterceptor"/>
"prepare" class="com.opensymphony.xwork.interceptor.PrepareInterceptor"/>
"conversionError" class="com.opensymphony.webwork.interceptor.WebWorkConversionErrorInterceptor"/>
"fileUpload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"/>
"execAndWait" class="com.opensymphony.webwork.interceptor.ExecuteAndWaitInterceptor"/>
"defaultStack">
"servlet-config"/>
"prepare"/>
"static-params"/>
"params"/>
"conversionError"/>
"validationWorkflowStack">
"defaultStack"/>
"validation"/>
"workflow"/>
"fileUploadStack">
"fileUpload"/>
"defaultStack"/>
"componentStack">
"component"/>
"defaultStack"/>
"modelDrivenStack">
"model-driven"/>
"defaultStack"/>
"chainStack">
"chain"/>
"defaultStack"/>
"executeAndWaitStack">
"defaultStack"/>
"execAndWait"/>
"completeStack">
"prepare"/>
"servlet-config"/>
"chain"/>
"model-driven"/>
"component"/>
"fileUpload"/>
"static-params"/>
"params"/>
"conversionError"/>
"validation"/>
"workflow"/>
解决方法就是:
1.不实现Preparable接口,但有 prepare() 方法。
Public class myAction extends BaseAction implements Preparable{
Public void prepare() throws Exception {
}
}
2.实现Preparable接口,但配置文件中不引用任何 调用prepare方法的拦截器。
配置实现:
以前是小弟的个人观点,不当之处还请多多赐教。