com.opensymphony.xwork2.interceptor
public class: ScopedModelDrivenInterceptor [javadoc |
source]
java.lang.Object
com.opensymphony.xwork2.interceptor.AbstractInterceptor
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor
All Implemented Interfaces:
Interceptor
An interceptor that enables scoped model-driven actions.
This interceptor only activates on actions that implement the
ScopedModelDriven interface. If
detected, it will retrieve the model class from the configured scope, then provide it to the Action.
Interceptor parameters:
- className - The model class name. Defaults to the class name of the object returned by the getModel() method.
- name - The key to use when storing or retrieving the instance in a scope. Defaults to the model
class name.
- scope - The scope to store and retrieve the model. Defaults to 'request' but can also be 'session'.
Extending the interceptor:
There are no known extension points for this interceptor.
Example code:
<-- Basic usage -->
<interceptor name="scopedModelDriven" class="com.opensymphony.interceptor.ScopedModelDrivenInterceptor" />
<-- Using all available parameters -->
<interceptor name="gangsterForm" class="com.opensymphony.interceptor.ScopedModelDrivenInterceptor">
<param name="scope">session</param>
<param name="name">gangsterForm</param>
<param name="className">com.opensymphony.example.GangsterForm</param>
</interceptor>
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor Detail: |
public String intercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof ScopedModelDriven) {
ScopedModelDriven modelDriven = (ScopedModelDriven) action;
if (modelDriven.getModel() == null) {
ActionContext ctx = ActionContext.getContext();
ActionConfig config = invocation.getProxy().getConfig();
String cName = className;
if (cName == null) {
try {
Method method = action.getClass().getMethod(GET_MODEL, new Class[0]);
Class cls = method.getReturnType();
cName = cls.getName();
} catch (NoSuchMethodException e) {
throw new XWorkException("The " + GET_MODEL + "() is not defined in action " + action.getClass() + "", config);
}
}
String modelName = name;
if (modelName == null) {
modelName = cName;
}
Object model = resolveModel(objectFactory, ctx, cName, scope, modelName);
modelDriven.setModel(model);
modelDriven.setScopeKey(modelName);
}
}
return invocation.invoke();
}
|
protected Object resolveModel(ObjectFactory factory,
ActionContext actionContext,
String modelClassName,
String modelScope,
String modelName) throws Exception {
Object model = null;
Map< String, Object > scopeMap = actionContext.getContextMap();
if ("session".equals(modelScope)) {
scopeMap = actionContext.getSession();
}
model = scopeMap.get(modelName);
if (model == null) {
model = factory.buildBean(modelClassName, null);
scopeMap.put(modelName, model);
}
return model;
}
|
public void setClassName(String className) {
this.className = className;
}
|
public void setName(String name) {
this.name = name;
}
|
public void setObjectFactory(ObjectFactory factory) {
this.objectFactory = factory;
}
|
public void setScope(String scope) {
this.scope = scope;
}
|