Spring自带的JPetStore分析(一)

Spring MVC + Spring + iBATIS 或Struts MVC + Spring +iBATIS
使用的是Spring自带的例子,与iBATIS给出的例子还有有区别
================
安装:
我使用的环境是Mysql,Tomcat4
按照readme的说明编译好war文件后,根据两个文件创建数据库表和插入数据,配置jdbc,会发现WEB-INF下面没有东西,其实所有的servlet被打包到jpetstore.jar里面去了。为了试验,我将这个jar包删除,用eclipse重新编译了一下。
--------------
使用Spring自带的Jpetstore,MVC使用了2种,spring的和struts的,默认的MVC框架是spring的。
如果要使用struts的,配置web.xml,将其中的*.do的映射改为struts的ActionServlet(将被注释的部分改掉就可以了)
数据持久层使用的是iBATIS。

注意到所有的jsp页面都放在WEB-INF下面,通过servlet访问,这样就不能直接通过地址直接访问jsp页面,提高了安全性。
先分析一下使用了Struts MVC的部分。
index.html首页给出了到shop/index.do的链接。
Struts-config.xml中
  <action path="/shop/index" type="org.springframework.samples.jpetstore.web.struts.DoNothingAction" validate="false">
    <forward name="success" path="/WEB-INF/jsp/struts/index.jsp"/>
  </action>
其中DonotiongAction返回ActionForward对象success。为什么这里不直接<action path="/shop/index" forward="/WEB-INF/jsp/struts/index.jsp"/>呢?可能是考虑规范格式的问题吧。(经试验,这种方式访问没有问题)

以下根据从访问首页开始到登录完成这么一个过程来了解一下具体的过程。(采用的是Struts的mvc框架)
====index.jsp===
采用了taglib jstl。
页面由4部分组成,IncludeTop.jsp,本页内容,IncludeBanner.jsp,IncludeBottom.jsp。
在IncludeTop.jsp中利用taglib获取session中的用户数据。根据是否有数据显示相应的内容。
===SignonForm.jsp===
登录在IncludeTop.jsp中有链接,定义如下
<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>
也是一个直接访问jsp的跳转。
SignonForm.jsp就是一个表单<form action="<c:url value="/shop/signon.do"/>" method="POST">
配置中
<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>
===SignonAction===
这个SignonAction继承了BaseAction
这个servlet还有一个功能就是如果链接带有参数signoff的话,可以登出,也就是让session失效
从actionForm中获取用户名和密码
Account account = getPetStore().getAccount(username, password);
这行语句是获取帐号信息的,这里开始使用到spring和BO(business object)了。
如果没有account返回,则request.setAttribute("message","Invalid username or password.  Signon failed.");并查找forward “failure”,应该是全局forward
<forward name="failure" path="/WEB-INF/jsp/struts/Error.jsp" redirect="false"/>
如果获得了account,则创建一个新的AccountActionForm accountForm并存入session中。返回首页或form中的forward。(form中的forward在没有登录或权限不够时,保存当前路径,提示登录,在登录完之后获得权限返回操作的页,例如:在没有登录的情况下在地址栏输入shop/editAccountForm.do,将转向到登录页面,登录后自动返回shop/editAccountForm.do。这个功能是依靠public abstract class SecureBaseAction extends BaseAction 来控制,在这个action中的execute方法中进行权限的验证)
---------------
getPetStore()是在BaseAction中定义的,返回一个org.springframework.samples.jpetstore.domain.logic.PetStoreFacade,这是个接口。
在BaseAction中定义了private PetStoreFacade petStore;,后面有个public void setServlet(ActionServlet actionServlet),重载了Action的该方法,struts会在创建Action实例的时候自动执行这个方法。(见struts api doc)(有个问题,不知道这个petStore是否是线程安全的。)
setServlet通过spring来获取PetStoreFacade对应的实现
ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.petStore = (PetStoreFacade) wac.getBean("petStore");
来给petStore初始化。
在spring配置文件applicationContext.xml中有如下定义
<bean id="petStore" parent="baseTransactionProxy">
 <property name="target">
  <bean class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
   <property name="accountDao"><ref bean="accountDao"/></property>
   <property name="categoryDao"><ref bean="categoryDao"/></property>
   <property name="productDao"><ref bean="productDao"/></property>
   <property name="itemDao"><ref bean="itemDao"/></property>
   <property name="orderDao"><ref bean="orderDao"/></property>
  </bean>
 </property>
</bean>
通过spring向PetStoreImpl注入了5个Dao。
===PetStoreImpl===
这个是本程序主要的一个business object。
通过5个Dao提供的功能来获得所需的数据
以account为例
先由petStore bean的配置文件
<property name="accountDao"><ref bean="accountDao"/></property>
注入accountDao,这个的定义为
 <bean id="accountDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao">
  <property name="sqlMapClient"><ref local="sqlMapClient"/></property>
 </bean>
而org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao利用了spring的模版,将数据库查询封装了起来
public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao
其中SqlMapClientDaoSupport 是spring的模版,这里通过AccountDao这个接口实现了依赖的注入。
模版根据iBATIS的配置文件来查询,这里是Account.xml
这样就获得了Account对象

罗罗嗦嗦了一点,下次分析其它部分。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值