SSH整合步骤

1、在Eclipse中新建WEB工程

2、添加所需jar
        

3、配置Struts2:拷贝一个struts.xml模板到src目录,并适当修改,在web.xml添加Struts2所需的配置代码
         struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
<struts>
     <!-- 配置为开发模式 -->
     <constant name="struts.devMode" value="true" />
   
    <!-- 配置扩展名为action -->
     <constant name="struts.action.extension" value="action" />
     
     <!-- 当struts配置文件修改时,系统是否自动重新加载该文件,默认false,开发阶段最好打开 -->
     <constant name="struts.configuration.xml.reload" value="true" />
     <!-- 全局视图 -->
     <package name="default" namespace="/" extends="struts-default">
     <global-results>
         <result name="error">/error.jsp</result>
         <result name="ajax">/ajax.jsp</result>
         <result name="noLogin">/login.jsp</result>
     </global-results>
     
</package>
</struts>
        web.xml添加如下代码:
  <!-- 配置Struts2过滤器 -->
     <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>*.action</url-pattern>
     </filter-mapping>

4、配置Spring:拷贝一个applicationContext.xml模板到 WEB-INF目录下,配置SessionFactory,并在web.xml添加Spring所需的配置代码,这里把数据库连接相关的信息配置在Hibernate中。
         applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
      xmlns:aop="http://www.springframework.org/schema/aop 
     xmlns:context="http://www.springframework.org/schema/context
      xmlns:tx="http://www.springframework.org/schema/tx
     <!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
     <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="prototype">
         <!-- 注入Hibernate 配置文件路径,前面要加上 classpath: -->
         <property name="configLocation" value="classpath:hibernate.cfg.xml" />
     </bean>

</beans>
        在web.xml中添加Spring支持:
<!-- 配置Spring监听器 -->
     <listener>
         <!--配置Spring的上下文加载监听 -->
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

5、配置Hibernate,拷贝一个hibernate.cfg.xml模板到src目录,并适当修改,在web.xml添加Hibernate所需的配置代码
         hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<hibernate-configuration>
    <session-factory>
         <!-- 数据库连接配置 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/staff?</property>
       
        <!-- 设置默认的数据库连接池 -->
        <property name="connection.pool_size">5</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
       
        <!-- 根据schema更新数据表的工具 -->
        <property name="hbm2ddl.auto">update</property>
        
        <!-- 数据表映射配置文件 -->
        <mapping resource="xxx.hbm.xml"/>
       
        <event type="pre-insert"/>
    </session-factory>
</hibernate-configuration>
         在web.xml中添加Hibernate支持:
      <!-- hibernate数据访问session -->
     <filter>
         <filter-name>openSessionInView</filter-name>
          <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>openSessionInView</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值