SSH——总结

正文

  还记得额之前本小菜写的关于SSH框架的总结吗?那个时候刚开始接触SSH,对其了解也都是自己从网上查的,其实查到的有些资料还是挺好的,可是资料再好,我看不懂也白搭,虽然看不懂但还是要继续进行的,只能在慢慢的摸索中不断的查不断的联想不断的试验才能让自己从“小白”变成“小小懂”。
  最近做完网上商城,对SSH框架有了一点新的理解,今天想和大家分享一下,有错的地方欢迎大家指正!

◆SSH框架

1.之前的理解
  刚开始我只知道SSH框架的系统会分层,从职责上分为表示层、业务层、数据持久层,其中Struts代表表示层,Spring代表业务层,Hibernate代表数据持久层。 我就这样错误的理解而又简单的理解着他们每一个简单的代表一层,跟着视频学习了一小段时间我再去看“SSH框架百科”发现自己刚开始的理解好搞笑,为啥刚开始也看了百科,同样的字样我咋就理解不了,非得实操一段时间再去理解这些概念的知识才能消化。
2.现在的理解
  SSH是一个由Struts、Spring、Hibernate集成的框架,之所以说SSH是一个集成框架,是因为Struts、Spring、Hibernate他们自身也是一个个框架,他们在这个集成框架中各自担负一些职责让这个集成框架更好的面对用户以供用户使用。
  其中:Struts是系统的整体架构,负责MVC的分离,在Struts框架的模型部分控制业务跳转;Hibernate是对持久层提供支持;Spring则在集成框架中做管理,管理Struts和Hibernate。



◆配置文件

1.Struts.xml

   大家都知道在表示层中,我们是先通过JSP页面实现交互界面,负责接收请求和传送响应,然后Struts根据struts.xml将ActionServlet接收到的请求委派给相应的Action处理。
   struts.xml主要就是配置一下自定义的拦截器,以及Action的配置。在struts.xml中Action的配置,根据小菜目前的理解,这块主要是跳转,可将其分为两种:第一种跳转到JSP,第二种是需要重定向时访问Action中的方法

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.devMode" value="false" />

    <package name="shop" extends="struts-default" namespace="/">
        <!--====================== 配置自定义拦截器===================== -->
        <interceptors>
            <interceptor name="privilegeInterceptor" class="cn.itcast.shop.interceptor.PrivilegeInterceptor"/>
        </interceptors>

        <!--全局-->
        <global-results>
            <result name="msg">/WEB-INF/jsp/msg.jsp</result>
            <result name="login">/admin/index.jsp</result>
        </global-results>

        <!--==========================配置Action===================-->
        <!-- 订单的Action -->
        <!--order_*中的*是通配符-->
        <!--type="redirectAction"表示需要重定向-->
        <!--method={1}:
        method——用于指定访问当前action时要调用的方法
        {1}——代表order_*中的*(通配符),假如加入访问路径是/order_findByUid.action,则“*==findByUid”,此刻访问的就是该Action中的findByUid方法。
        当name中含有多个通配符的时候,method={2} ,就代表第二个通配符,后边的就以此类推。
这种方式更灵活的简化了struts.xml的配置文件。-->
        <action name="order_*" class="orderAction" method="{1}">
            <result name="saveOrder">/WEB-INF/jsp/order.jsp</result>
            <result name="findByUid">/WEB-INF/jsp/orderList.jsp</result>
            <result name="findByOid">/WEB-INF/jsp/order.jsp</result>
            <result name="updateStateSuccess" type="redirectAction">order_findByUid.action?page=1</result>
        </action>

    </package>
</struts>


2.applicationContext.xml

2.1简析
  ApplicationContext.xml是Spring的默认配置文件,用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的“图纸”。


spring中的bean标签
  在配置文件中大家可以看到好多的bean,一个bean相当于一个组件,用于具体实现某个功能,如何实现呢?——关联它,然后set注入。

spring中的scope
是什么——
  scope是对象在spring容器中的生命周期,也可以说是对象在spring容器中的创建方式。
可取的值——
  singleton:每次都访问同一个实例。
  prototype:容器每次返回请求方该对象的一个新实例
  request、session、global session:用于web程序
2.1代码

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- =====================================1、加载JDBC配置文件,建立数据源=================================================================-->
    <!-- 引入外部属性文件 -->
    <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>


    <!-- ======================================2、Hibernate的相关信息========================================= =====-->
    <!-- 定义hibernate会话工厂,并注入数据源实例DataSource -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置Hibernate的其他的属性 -->
        <property name="hibernateProperties">
            <props>
                <!-- hibernate.dialect只是hibernate使用的数据库方言,就是要用hibernate连接哪种类型的数据库服务器 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 是否在后台显示hibernate用到的SQL语句,开发时设置为true,项目部署后可以设置为false,提高运行效率 -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
                <!--是否自动创建数据库表,主要的值:  -->
                <!-- validate:当sessionFactory创建时,自动验证或者schema定义导入数据库 -->
                <!-- create:每次启动都drop掉原来的schema,创建新的 -->
                <!-- create-drop:当sessionFactory明确关闭时,drop掉schema -->
                <!-- update(常用):如果么有schema就创建,有就更新 -->
                <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>
                <value>cn/itcast/shop/order/vo/Order.hbm.xml</value>
                <value>cn/itcast/shop/order/vo/OrderItem.hbm.xml</value>
                <value>cn/itcast/shop/adminuser/vo/AdminUser.hbm.xml</value>
            </list>
        </property>
    </bean>



    <!-- ====================================3、事务管理: 与hibernate相关联==================================-->
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!--========================================= 4、开启注解事务:事务的配置========================================== -->
    <tx:annotation-driven transaction-manager="transactionManager"/>


    <!--================================================== 5、Action的配置 ==================================-->
    <!-- 首页访问的Action -->
    <bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction" scope="prototype">
        <!-- 注入用户模块的Service -->
        <property name="categoryService" ref="categoryService"/>
        <!-- 注入用户模块的Service -->
        <property name="productService" ref="productService"/>
    </bean>

    <!-- 用户模块的Action -->
    <bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype">
        <!-- 注入用户模块的业务层Service -->
        <property name="userService" ref="userService"/>
    </bean>



    <!-- ===========================6、Service的配置====== ===========================-->
    <bean id="userService" class="cn.itcast.shop.user.service.UserService">
        <!-- 注入用户模块的数据持久层DAO -->
        <property name="userDao" ref="userDao"/>
    </bean>



    <!-- ==========================7、Dao的配置 ================== ===========================-->
    <bean id="userDao" class="cn.itcast.shop.user.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>



小结

   小菜今天的分享就到这里,还有很多地方理解的不到位,也有个别是自己目前的见解,也有很多是参考的大神的博客或者百科,有不周全的欢迎各位指出交流!

猜你喜欢

转载自blog.csdn.net/whm18322394724/article/details/80800022
今日推荐