码迷,mamicode.com
首页 > 其他好文 > 详细

SSH整合的详细步骤

时间:2017-10-16 01:55:40      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:url   struts2   tran   映射   expr   持久化   factory   package   关系   

SSH整合

新建一个动态web工程-->加入Spring-->加入Hibernate-->加入Struts2

1.在 web中应用Spring
  目的:在web应用程序加载成功之后,就可以使用Spring IOC容器
  (1)加入jar包
    除了Spring所必须的jar包之外,还有两个jar包必须加入
    spring-webmvc-4.0.0.RELEASE.jar        spring-web-4.0.0.RELEASE.jar
  (2)在web.xml文件中,Spring提供的ContextLoaderListener启动Spring IOC容器
    <context-param>
    <!-- 在当前web应用的初始化参数中配置Spring配置文件的路径 -->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
2.加入Spring
  (1)加入jar包
    包括Spring所必须的jar包,还有配置数据源所需的jar包
  (2)在类路径下创建Spring的配置文件applicationContext.xml,在配置文件中,配置数据源和Spring的声明式事务
    <!-- 配置数据源,数据源的属性在外部资源文件中设置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
      <property name="user" value="${jdbc.user}"></property>
      <property name="password" value="${jdbc.password}"></property>
      <property name="jdbcUrl" value="${jdbc.url}"></property>
      <property name="driverClass" value="${jdbc.driverClass}"></property>
      <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
      <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    <!-- 配置Spring的声明式事务 -->
    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="hibernateTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
      <property name="sessionFactory" ref="localSessionFactoryBean"></property><!-- ref属性的值是加入Hibernate时配置的SessionFactory的bean的id -->
    </bean>
    <tx:advice id="adviceDjp" transaction-manager="hibernateTransactionManager">
      <tx:attributes>
        <tx:method name="get*" read-only="true"/>
        <tx:method name="*"/>
      </tx:attributes>
    </tx:advice>
    <aop:config>
      <aop:pointcut expression="execution(* *.*(..))" id="pointcutDjp"/>
      <aop:advisor advice-ref="adviceDjp" pointcut-ref="pointcutDjp"/>
    </aop:config>
3.加入Hibernate
  (1)在类路径下创建Hibernate的配置文件hibernate.cfg.xml,配置Hibernate的一些基本信息
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
  (2)创建持久化类以及对应的对象-关系映射文件(.hbm.xml)
  (3)在Spring的配置文件applicationContext.xml中配置SessionFactory实例
    <!-- 配置sessionFactory -->
    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="localSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
      <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
      <property name="mappingLocations" value="classpath:com/…/…/*.hbm.xml"></property>
    </bean>
4.加入Struts2
  (1)加入jar包
    除了Struts2所必须的jar包之外,还有一个jar包必须加入 struts2-spring-plugin-2.5.12.jar
  (2)在web.xml文件中加入Struts2的Filter
    <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  (3)创建Action类,并在Spring IOC容器中配置Struts2的Action实例
    <bean class="……" id="……" scope="prototype">
      ……
    </bean>
    注意:由于Struts2的Action不是单例的,所以,在applicationContext.xml中配置时,必须修改scope属性(该属性默认singleton)
  (4)在类路径下创建Struts2 的配置文件struts.xml
    <struts>
      <constant name="struts.enable.DynamicMethodInvocation" value="false" />
      <constant name="struts.devMode" value="true" />
      <package name="default" namespace="/" extends="struts-default">
        <action name="……" class="……" method="……">
          <result name="……">……</result>
        </action>
      </package>
    </struts>
    注意:struts.xml中<action>的class属性的值是applicationContext.xml中对应的<bean>的id属性的值

SSH整合的详细步骤

标签:url   struts2   tran   映射   expr   持久化   factory   package   关系   

原文地址:http://www.cnblogs.com/duanjiapingjy/p/7675173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!