paramsPrepareParamsStack
我觉得在讲解标题内容之前有必要在了解一下我们所谓的Action到底有哪些功能,
1:通过实现xxx Aware 可以使Action内部获得属性
2:XXXCapable可以给struts环境提供属性
3:实现特定的接口以供拦截器调用
4:自身的action方法
5:Javabean
该拦截器栈的执行大致过程为:params---prepare---modeldriven---params
作用:可以先给栈顶对象的属性赋值,在根据栈顶对象的属性决定用modeldirven把那个对象压入栈顶,而prepare拦截器提供getModel方法的model,再对其进行赋值。
public StringdoIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
//判断action是否继承了Preparable
if (action instanceof Preparable) {
try {
String[] prefixes;
firstCallPrepareDo默认为0,故执行else,其数组为:prepare,preparedo;
if (firstCallPrepareDo) {
prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
} else {
prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
}
///调用函数
PrefixMethodInvocationUtil.invokePrefixMethod(invocation,prefixes);
}
catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else if(cause instanceof Error) {
throw (Error) cause;
} else {
throw e;
}
}
/ alwaysInvokePrepare默认为1,执行action实现的prepare方法;
if (alwaysInvokePrepare) {
((Preparable)action).prepare();
}
}
return invocation.invoke();
}
}
public static void invokePrefixMethod(ActionInvocationactionInvocation, String[] prefixes) throws InvocationTargetException,IllegalAccessException {
Object action =actionInvocation.getAction();
String methodName =actionInvocation.getProxy().getMethod();
if (methodName == null) {
methodName = DEFAULT_INVOCATION_METHODNAME;
}
///执行getPrefixedMethod来获得Method;
Method method = getPrefixedMethod(prefixes,methodName, action);
if (method != null) {
method.invoke(action, new Object[0]);
}
public static Method getPrefixedMethod(String[] prefixes, StringmethodName, Object action) {
assert(prefixes != null);
String capitalizedMethodName = capitalizeMethodName(methodName);
for (String prefixe : prefixes) {
获得prepare[mothedname]方法;
String prefixedMethodName = prefixe+ capitalizedMethodName;
try {
returnaction.getClass().getMethod(prefixedMethodName, EMPTY_CLASS_ARRAY);
}
catch (NoSuchMethodException e) {
// hmm -- OK, try next prefix
if (LOG.isDebugEnabled()) {
LOG.debug("cannot find method [#0] in action[#1]",prefixedMethodName, action.toString());
}
}
}
return null;
}