JPetStore学习备忘录1

本文介绍了如何配置Struts框架中的BeanAction组件,并详细解析了BeanAction中的三种映射方式:通过parameter属性指定方法名、通过路径参数决定方法名以及默认调用全局成功转发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1数据导入

下载安装包,根据自带mysql脚本导入数据。在项目中加入mysql jdbc驱动。修改Src/Properties/Database.properties为
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jpetstore
username=root
password=root
(也可不做修改,使用安装包下自带的的数据库及驱动hsqldb.jar)

2关于actionbean的初步理解

源代码在lib下的beanaction.jar里,以下红色表示3种映射方式
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.");
        }
      }
      BaseBean bean = (BaseBean) form;
      ActionContext.initCurrentContext(request, response);
      if (bean != null) {
        // Explicit Method Mapping:如果parameter不为空且不为“*”,方法名由parameter本身决定
        //如    struts-config.xml中
    
<action path="/shop/switchSearchListPage" type="org.apache.struts.beanaction.BeanAction"></action>            name="catalogBean" scope="session" parameter="switchProductListPage"
            validate="false">
      <forward name="success" path="/catalog/SearchProducts.jsp"></forward>
   

        Method method = null;
        String methodName = mapping.getParameter();
        if (methodName != null && !NO_METHOD_CALL.equals(methodName)) {
          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 method parameter ('" + methodName + "').  Cause: " + e, e);
          }
        }

 

        // Path Based Method Mapping:如果parameter为空,方法由“/”后面带的参数决定
       //如struts-config.xml中
    <action path="/shop/viewCategory" type="org.apache.struts.beanaction.BeanAction"></action>            name="catalogBean" scope="session"
            validate="false">
      <forward name="success" path="/catalog/Category.jsp"></forward>
   

        if (method == null && !NO_METHOD_CALL.equals(methodName)) {
          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);
//如果parameter为“*”直接返回全局forward SUCCESS_FORWARD.如struts-config.xml中
    <action path="/shop/index" type="org.apache.struts.beanaction.BeanAction"></action>            name="catalogBean" parameter="*" validate="false">
      <forward name="success" path="/catalog/Main.jsp"></forward>
   

  }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值