jpetstore的两个核心类:BeanAction和BaseBean,在这儿对BeanAction进行一下解析,源码:
public class BeanAction extends Action {
private static final String NO_METHOD_CALL = "*";
private static final String SUCCESS_FORWARD = "success";
public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String forward = SUCCESS_FORWARD;
try {
if (!(form instanceof BaseBean)) {
if (form != null) {
throw new BeanActionException("The form for mapping '" + mapping.getPath() + "' named '" + mapping.getName() + "' was not an instance of BaseBean. BeanAction requires an BaseBean instance.");
} else {
throw new BeanActionException("The form for mapping '" + mapping.getPath() + "' named '" + mapping.getName() + "' was null. BeanAction requires an BaseBean instance.");
}
}
//实例化bean,jpetstore中的bean也是VO,里面有相应的行为操作
BaseBean bean = (BaseBean) form;
//初始化当前应用上下文环境
ActionContext.initCurrentContext(request, response);
if (bean != null) {
// Explicit Method Mapping
Method method = null;
//得到配置文件设定的参数值
String methodName = mapping.getParameter();
if (methodName != null && !NO_METHOD_CALL.equals(methodName)) { // x1
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
//执行Bean里的方法,并返回方法的返回字符串
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
} catch (Exception e) {
throw new BeanActionException("Error dispatching bean action via method parameter ('" + methodName + "'). Cause: " + e, e);
}
}
// Path Based Method Mapping
if (method == null && !NO_METHOD_CALL.equals(methodName)) { // x2
methodName = mapping.getPath();
if (methodName.length() > 1) {
int slash = methodName.lastIndexOf("/") + 1;
methodName = methodName.substring(slash);
if (methodName.length() > 0) {
try {
method = bean.getClass().getMethod(methodName, null);
synchronized (bean) {
forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
}
} catch (Exception e) {
throw new BeanActionException("Error dispatching bean action via URL pattern ('" + methodName + "'). Cause: " + e, e);
}
}
}
}
}
} catch (Exception e) {
forward = "error";
request.setAttribute("BeanActionException", e);
}
return mapping.findForward(forward);
}
}
BeanAction的execute()方法运行场景 :
1. 当请求url对应的action配置文件中带有parameter时,例如<a href="switchProductListPage.shtml?pageDirection=next"></a>链接,先查找配置文件中该aciton项的parameter属性是否已定义,如果有定义:
<action path="/shop/switchSearchListPage" type="org.apache.struts.beanaction.BeanAction"
name="catalogBean" scope="session" parameter="switchProductListPage"
validate="false">
<forward name="success" path="/catalog/SearchProducts.jsp"/>
</action>
程序将会运行x1处的代码,执行catalogBean中的switchProductListPage方法,
2. 如果请求的action的pararmeter属性未定义,如
<html:link page="/shop/viewCategory.shtml?categoryId=FISH">这样的链接,配置文件中没有parameter属性,
<action path="/shop/viewCategory" type="org.apache.struts.beanaction.BeanAction"
name="catalogBean" scope="session"
validate="false">
<forward name="success" path="/catalog/Category.jsp"/>
</action>
程序将会运行x2处的代码,查找path中的最后一个“/”后的单词viewCategory,执行catalogBean中的viewCategory方法。
3. 第三种情况是提交的url对应的action在配置文件中的parameter="*"时,如<a href="shop/index.shtml">Enter the Store</a>这样的链接,在配置文件中
<action path="/shop/index" type="org.apache.struts.beanaction.BeanAction"
name="catalogBean" parameter="*" validate="false">
<forward name="success" path="/catalog/Main.jsp"/>
</action>
程序将不会运行x1和x2处的代码,直接返回已定义好的forward页面。