首先想介绍的是struts2的原型驱动ModelDriven机制。
所谓的ModelDriven,就是把一个实体类当成页面数据的收集对象。用法看起来像下面这个样子
<span style="font-size:14px;">public class UserAction implements ModelDriven{
private User user;
@Override
public Object getModel() {
if(user == null){
user = new User();
}
return user;
}
}
做法是利用ModelDriven机制,让UserAction实现一个ModelDriven接口,同时实现接口中的方法:getModel()
这个时候user就可以自动收集页面中传过来的数据。
那么 ModelDriven背后的机制到底是什么?
ModelDriven背后的机制其实就是ValueStack。界面中传过来的数据能够被直接赋值给user对象,这就说明user对象是ValueStack中的根对象(Root对象)!
那么,user对象什么时候出现在valueStack中并且为什么会出现在valueStack中呢?
一切原因源自于ModelDrivenInterceptor。ModelDrivenInterceptor是缺省的拦截器链(struts-default)的一部分,当一个请求经过ModelDrivenInterceptor的时候,在这个拦截器中,会判断当前要调用的Action对象是否实现了ModelDriven接口,如果实现了这个接口,则调用getModel()方法,并把返回值压入ValueStack。
请看以下ModelDrivenInterceptor一部分源码:
@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); //注意:只有当原型对象不为null的时候,才会被压入栈顶。
}
if (refreshModelBeforeResult) { //这是个很有用处的地方
invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
}
}
return invocation.invoke();
}
现在假设我们有一个业务处理流程是这样 :
public class UserAction implements ModelDriven{
private User user;
@Override
public Object getModel() {
if(user == null){
user = new User();
}
return user;
}
public String update(){
user = new UserManager().findUserById(user.getId());
user.setUsername("old");
return "update"; }
refreshModelBeforeResult属性默认为false。struts2的是这样描述这个属性的:
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.
意思就是说:假如原型实例改变了,在action被执行之后,如果你想刷新在值栈中的原型实例,那么就把这个属性设置true,这样getModel()
就会被调用两次。具体实现就是先把旧的model对象从ValueStack中移除,然后再把新的model对象压进ValueStack!
看以下源码:
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.equals(newModel)) {
needsRefresh = false;
break;
}
}
// 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);
}
}
}
转载自(http://www.bubuko.com/infodetail-201255.html)