com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xwork Show documentation
Show all versions of xwork Show documentation
XWork is an command-pattern framework that is used to power WebWork
as well as other applications. XWork provides an Inversion of Control
container, a powerful expression language, data type conversion,
validation, and pluggable configuration.
The newest version!
/*
* 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;
/**
*
*
* Watches for {@link ModelDriven} actions and adds the action's model on to the value stack.
*
* Note: The ModelDrivenInterceptor must come before the both {@link StaticParametersInterceptor} and
* {@link ParametersInterceptor} if you want the parameters to be applied to the model.
*
* Note: The ModelDrivenInterceptor will only push the model into the stack when the
* model is not null, else it will be ignored.
*
*
*
* Interceptor parameters:
*
*
*
*
*
* - 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.
*
*
*
*
*
* Extending the interceptor:
*
*
*
*
*
* There are no known extension points to this interceptor.
*
*
*
* Example code:
*
*
*
* <action name="someAction" class="com.examples.SomeAction">
* <interceptor-ref name="modelDriven"/>
* <interceptor-ref name="basicStack"/>
* <result name="success">good_result.ftl</result>
* </action>
*
*
*
* @author tm_jee
* @version $Date: 2008-06-21 11:29:39 +0200 (Sat, 21 Jun 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);
}
}
}
}
}