JPetStore学习备忘录1

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>
   
  }
}


 

你可能感兴趣的:(bean,mysql,jsp,struts,jdbc)