UML软件工程组织

基于Spring例子的JPetStore分析
  这几天一直在学习JPetStore这个基与轻量级j2EE架构的宠物电子商务网站,下面来分析一下基于Struts+Spring+Ibatis架构的用户管理模块.
  
  首先分析一下jpetstore的用户登录界面,看struts-config.xml文件,
  
  用户信息Bean,用户信息Bean为AccountActionForm配置两个不同的实例。accountForm用户存放用户登录信息。workingAccountForm用于用户注册,以及账号修改时存放信息。
  
  <form-beans>
  <!--存放用户登陆的账号信息-->
  <form-bean name="accountForm" type="org.springframework.samples.jpetstore.web.struts.AccountActionForm"/>
  <form-bean name="cartForm" type="org.springframework.samples.jpetstore.web.struts.CartActionForm"/>
  <form-bean name="emptyForm" type="org.springframework.samples.jpetstore.web.struts.BaseActionForm"/>
  <!--用于用户注册和个人资料修改时存放用户信息-->
  <form-bean name="workingAccountForm" type="org.springframework.samples.jpetstore.web.struts.AccountActionForm"/>
  <form-bean name="workingOrderForm" type="org.springframework.samples.jpetstore.web.struts.OrderActionForm"/>
  </form-beans>
  
  1.使用已有帐号登陆
  
  <action path="/shop/signonForm" type="org.springframework.samples.jpetstore.web.struts.DoNothingAction"
  validate="false">
  <forward name="success" path="/WEB-INF/jsp/struts/SignonForm.jsp"/>
  </action>
  
  <action path="/shop/signon" type="org.springframework.samples.jpetstore.web.struts.SignonAction"
  name="accountForm" scope="session" validate="false">
  <forward name="success" path="/shop/index.do"/>
  </action>
  
  <!-- 用户点击登陆,系统调用 shop/signonForm 直接将用户的登陆请求,转向到SignonForm.jsp页面(登陆界面),输入用户名,密码,点击登录,系统将调用 shop/signon Action来处理用户登录请求,如果登陆失败,页面返回到SignonForm.jsp页面(登陆界面),登陆成功,shop/signon 转到主页面shop/index.do。--〉
  
  2.创建新帐号
  
  <!-- 如果用户在当前登录页面(SigonForm.jsp)中选择“创建新帐号”,系统将调用“shop/newAccountForm”在NewAccountFormAction 的execute中为httpsession创建AccountActionForm用户存放用户的注册信息,然后转向到用户注册界面NewAccountForm.jsp -->
  
  <action path="/shop/newAccountForm" type="org.springframework.samples.jpetstore.web.struts.NewAccountFormAction"
  name="workingAccountForm" scope="session" validate="false">
  <forward name="success" path="/WEB-INF/jsp/struts/NewAccountForm.jsp"/>
  </action>
  
  <!--用户在填写完注册信息以后,注册,系统调用“NewAccountAction”,如果注册失败,返回注册界面,系统将显示注册的错误信息,如果注册成功,系统自动转向到主页面。-->
  <action path="/shop/newAccount" type="org.springframework.samples.jpetstore.web.struts.NewAccountAction"
  name="workingAccountForm" scope="session" validate="true" input="/WEB-INF/jsp/struts/NewAccountForm.jsp">
  <forward name="success" path="/shop/index.do"/>
  </action>
  
  3.编辑账号
  
  <!-- 当用户点击修改用户信息的时候,系统调用editAccountForm 为账号的修改做一些必要的准备,然后定向到账号修改页面EditAccountForm.jsp,用户输入修改,点击提交,系统调用shop/editAccount检查修改资料是否合法,如果没有错误,确认修改,转到主页面,若有错误,转到账号修改界面->
  
  <action path="/shop/editAccountForm" type="org.springframework.samples.jpetstore.web.struts.EditAccountFormAction"
  name="workingAccountForm" scope="session" validate="false">
  <forward name="success" path="/WEB-INF/jsp/struts/EditAccountForm.jsp"/>
  </action>
  
  <action path="/shop/editAccount" type="org.springframework.samples.jpetstore.web.struts.EditAccountAction"
  name="workingAccountForm" scope="session" validate="true" input="/WEB-INF/jsp/struts/EditAccountForm.jsp">
  <forward name="success" path="/shop/index.do"/>
  </action>
  
  个人分析:
  
  从jpetsore的账号管理的源代码来看,有以下几个值得我们注意的地方(目前对struts还不是很熟悉):
  
  1.AccountActionForm封装了账号Account,不知道是不是这个原因,需要在转入创建账号页面,或者是修改账号页面的情况下,在action的doExecute执行中都创建了AccountActionForm实例,并对其进行了初始化,并把它加入了httpsession中。
  
  2.系统用BaseActionForm继承了ActionFrom ,使用BaseAction继承了Action,同时把这两个子类替代了其父类在系统中的作用,所余的from和action都是从这两个派生类派生出来的。BaseActionFrom提供了方便的字段校验,而BaseAction加入了
  
  public void setServlet(ActionServlet actionServlet) {
  super.setServlet(actionServlet);
  if (actionServlet != null) {
  ServletContext servletContext = actionServlet.getServletContext();
  WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
  this.petStore = (PetStoreFacade) wac.getBean("petStore");
  }
  
  很好的和spring衔接在了一起,获得了系统的业务逻辑对象

版权所有:UML软件工程组织