jpetstore的BeanAction源码解析

      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页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值