Spring例子JPetStore分析---------1

博主学习JPetStore宠物电子商务网站,分析基于Struts+Spring+Ibatis架构的用户管理模块。介绍了用户登录、创建新帐号、编辑账号的流程及相关配置,还指出从源代码看,转入特定页面时会创建并初始化AccountActionForm实例,系统用派生类替代父类,方便与Spring衔接。

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

    这几天一直在学习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衔接在了一起,获得了系统的业务逻辑对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值