在使用Struts2的时候,使用了model-driven的形式(implements ModelDriven),但是在使用的过程中,缺发现没请求一次action则getModel方法都会被调用两次,这个真是让人郁闷的事情,当然也可能是我的是偶那个方式本身就是错误的,因为在getModel中的逻辑是:
查询数据库得到数据信息,
包装为一个model对象
返回包装后的对象
从上面的逻辑可知,如果getModel被调用两次的话,则也就意味着要做两次数据库请求哈!!
出现问题的资源文件的版本:
struts2-core-2.0.11.2.jar, xwork-2.0.4.jar
配置文件:
<action name="modelDrivenRequest" class="modelDrivenRequest">
<interceptor-ref name="myStack"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="params"/>
<result>/question/modeldriven.jsp</result>
</action>
后来经过跟踪ModelDrivenInterceptor的源代码发现:
if (modelDriven.getModel() != null) {
stack.push(modelDriven.getModel());
}
这种实现方式,大概是为了确保,stack.push操作的model对象是最新的吧,但这也正是getModel方法被调用了两次的原因,
后来考虑干脆不再使用modelDriven的实现形式,感觉这个东西的意义不大,尤其在如下形式的时候,更是不知所措:
implements
ModelDriven<List<MyData>> ,也就是model是一个集合的情况; 舍弃ModelDriven是我的选择,当然也可能是对它的理解有偏差的缘故吧,
最后为了解决调用getModel方法两次的问题,可以参看xwork-2.1.3的实现版本,这里给出它的源代码:
/*
* Copyright (c) 2002-2006 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
/**
* <!-- START SNIPPET: description -->
*
* Watches for {@link ModelDriven} actions and adds the action's model on to the value stack.
*
* <p/> <b>Note:</b> The ModelDrivenInterceptor must come before the both {@link StaticParametersInterceptor} and
* {@link ParametersInterceptor} if you want the parameters to be applied to the model.
*
* <p/> <b>Note:</b> The ModelDrivenInterceptor will only push the model into the stack when the
* model is not null, else it will be ignored.
*
* <!-- END SNIPPET: description -->
*
* <p/> <u>Interceptor parameters:</u>
*
* <!-- START SNIPPET: parameters -->
*
* <ul>
*
* <li>refreshModelBeforeResult - set to true if you want the model to be refreshed on the value stack after action
* execution and before result execution. The setting is useful if you want to change the model instance during the
* action execution phase, like when loading it from the data layer. This will result in getModel() being called at
* least twice.</li>
*
* </ul>
*
* <!-- END SNIPPET: parameters -->
*
* <p/> <u>Extending the interceptor:</u>
*
* <p/>
*
* <!-- START SNIPPET: extending -->
*
* There are no known extension points to this interceptor.
*
* <!-- END SNIPPET: extending -->
*
* <p/> <u>Example code:</u>
*
* <pre>
* <!-- START SNIPPET: example -->
* <action name="someAction" class="com.examples.SomeAction">
* <interceptor-ref name="modelDriven"/>
* <interceptor-ref name="basicStack"/>
* <result name="success">good_result.ftl</result>
* </action>
* <!-- END SNIPPET: example -->
* </pre>
*
* @author tm_jee
* @version $Date: 2008-06-21 17:29:39 +0800 (星期六, 21 六月 2008) $ $Id: ModelDrivenInterceptor.java 1833 2008-06-21 09:29:39Z rainerh $
*/
public class ModelDrivenInterceptor extends AbstractInterceptor {
protected boolean refreshModelBeforeResult = false;
public void setRefreshModelBeforeResult(boolean val) {
this.refreshModelBeforeResult = val;
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof ModelDriven) {
ModelDriven modelDriven = (ModelDriven) action;
ValueStack stack = invocation.getStack();
Object model = modelDriven.getModel();
if (model != null) {
stack.push(model);
}
if (refreshModelBeforeResult) {
invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
}
}
return invocation.invoke();
}
/**
* Refreshes the model instance on the value stack, if it has changed
*/
protected static class RefreshModelBeforeResult implements PreResultListener {
private Object originalModel = null;
protected ModelDriven action;
public RefreshModelBeforeResult(ModelDriven action, Object model) {
this.originalModel = model;
this.action = action;
}
public void beforeResult(ActionInvocation invocation, String resultCode) {
ValueStack stack = invocation.getStack();
CompoundRoot root = stack.getRoot();
boolean needsRefresh = true;
Object newModel = action.getModel();
// Check to see if the new model instance is already on the stack
for (Object item : root) {
if (item == newModel) {
needsRefresh = false;
}
}
// Add the new model on the stack
if (needsRefresh) {
// Clear off the old model instance
if (originalModel != null) {
root.remove(originalModel);
}
if (newModel != null) {
stack.push(newModel);
}
}
}
}
}
通过这个实现我们可以通过配置来决定是否调用getModel方法两次:
<action name="modelDrivenRequest" class="modelDrivenRequest">
<interceptor-ref name="myStack"/>
<interceptor-ref name="modelDriven213">
<param name="refreshModelBeforeResult">false</param>
</interceptor-ref>
<interceptor-ref name="params"/>
<result>/question/modeldriven.jsp</result>
</action>