简单解释SSH框架中Action、Service、Dao、struts、Spring之间的关系

来源:互联网 发布:js字符串拼接 编辑:程序博客网 时间:2024/05/19 02:24

首先,先看下ssh框架的目录结构:




这个是简单的SSH框架,由于之前学习过经典三层框架,现在结合着三层框架来理解下SSH框架中的各个层之间的关系:

一、Action

      看一下这个Action中的代码:这个类似于管理业务调度和管理跳转的,从这可以跳转到Service层,调用Service层中的方法,类似于三层结构中的B层,业务逻辑层。

<span style="font-family:KaiTi_GB2312;font-size:18px;">// 根据商品的ID进行查询商品:执行方法:public String findByPid() {// 调用Service的方法完成查询.product = productService.findByPid(product.getPid());return "findByPid";}</span>
二、Service

      看一下service代码,每个service代码中要有个ser方法,Service层是管理具体的功能,Action只负责管理,而Service负责实施。

<span style="font-family:KaiTi_GB2312;font-size:18px;">public class ProductService {// 注入productdaoprivate ProductDao productDao;public void setProductDao(ProductDao productDao) {this.productDao = productDao;}// 首页热门商品查询方法public List<product> findHot() {return productDao.findHot();}}</span>

三、Dao

     dao层相当于三层中的D层,这里连接数据库的操作用的是hibernate,完成增删改查。Hibernate的最大好处就是根据数据库的表,反向生成实体类,并且还有关系在里面,还有就是它对数据的操作也很方便;

<span style="font-family:KaiTi_GB2312;font-size:18px;">// 首页热门商品的查询public List<product> findHot() {// 使用离线条件查询DetachedCriteria criteria = DetachedCriteria.forClass(product.class);// 查询热门的商品criteria.add(Restrictions.eq("is_hot", 1));// 倒序排序输出:criteria.addOrder(Order.desc("pdate"));// 执行查询List<product> list = this.getHibernateTemplate().findByCriteria(criteria, 0, 10);return list;}</span>


四、struts——控制界面和Action之间的联系

<span style="font-family:KaiTi_GB2312;font-size:18px;"><constant name="struts.devMode" value="false" /><!-- <package name="SSH-shop" extends="struts-default" namespace="/WebRoot"> --><package name="SSH-shop" extends="struts-default" namespace="/"><global-results><result name="msg">/WEB-INF/jsppackage/msg.jsp</result><result name="login">/admin/index.jsp</result></global-results><!-- 配置首页访问的Action --><action name="index" class="indexAction"><result name="index">/WEB-INF/jsppackage/index.jsp</result></action></span>


五、applicationContext.xml——控制struts和后台逻辑之间的关系

     这里在Bean中进行了对Action的配置,和访问Service中的方法,在这里也就是spring,在service层和dao层都没有看到实例化某个类的带阿门,所以spring的出现,减少了各个类中的调用关系,将这些关系都转化到了配置文件中,这样就简便多了。

<span style="font-family:KaiTi_GB2312;font-size:18px;"><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 配置连接池: --><!-- 引入外部属性文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置C3P0连接池: --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.user}" /><property name="password" value="${jdbc.password}" /></bean><!-- Hibernate的相关信息 --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 注入连接池 --><property name="dataSource" ref="dataSource" /><!-- 配置Hibernate的其他的属性 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.connection.autocommit">false</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><!-- 配置Hibernate的映射文件 --><property name="mappingResources"><list><value>cn/itcast/shop/user/vo/User.hbm.xml</value><value>cn/itcast/shop/category/vo/Category.hbm.xml</value><value>cn/itcast/shop/product/vo/Product.hbm.xml</value><value>cn/itcast/shop/categorysecond/vo/CategorySecond.hbm.xml</value></list></property></bean><!-- 事务管理: --><!-- 事务管理器 --><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 开启注解事务 --><tx:annotation-driven transaction-manager="transactionManager" /><!-- Action的配置 =========================== --><!-- 首页访问的Action --><bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction"scope="prototype"><property name="categoryService" ref="categoryService" /><property name="productService" ref="productService" /></bean></span>

总结:

        这个小例子简单介绍了简单ssh框架是什么,怎么用的,还要继续深入学习,欢迎大家批评指正!


1 0
原创粉丝点击