spring3 struts2 hibernate3整合

此种整合思路是尽量把需要配置的东西整合进applicationContext.xml中

步骤:
1、创建工程,设置编码utf-8,jsp编码
2、导入jar包
3、工程下创建三个source文件夹:
src(已存在)
config 配置文件所在
test 测试类所在
4、src下创建持久化类+映射文件
5、在config中创建spring文件夹,写spring的配置文件,引入sessionFactory

<!-- 1.引入dbcp配置文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>
    <!-- 2.配置数据源参数 -->
    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
<!-- 3.1 第二种sessionFactory配置方法 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 指向了映射文件所在的路径 -->
        <property name="mappingDirectoryLocations">
            <list>
                <!-- spring容器会去该包及子包下搜索所有的映射文件 -->
                <value>classpath:com/itheima11/s2sh/domain</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

6、测试是否能建表
(只要new ClassPathXMLApplicationContext(“spring/applicationContext.xml”)即可了)

@Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }

7、创建dao,service层,并且在spring容器中配置,注意dao的实现类继承了HibernateDaoSupport,配置上有sessionFactory注入
8、在spring中声明事物处理配置:
事物管理器声明、事务策略、aop:advisor

    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <!-- 声明事物策略 -->
    <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 什么样方法采取什么策略 -->
            <tx:method name="save*" read-only="false" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.itheima11.s2sh.service.impl.*.*(..))"
            id="point1" />
        <aop:advisor advice-ref="tx" pointcut-ref="point1" />
    </aop:config>

9、写动作类
10、在spring中配置动作类,注意多例 scope=”prototype
11、测试Action类

@Test
    public void testAction() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        context.getBean("personAction");
    }

12、在config根目录:写struts配置文件

<?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.ui.theme" value="simple"></constant>
        <constant name="struts.devMode" value="true"/>
        <include file="struts/struts-person.xml"></include>
    </struts>

13、写上述包含的strut-person.xml

<?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>
        <package name="person" namespace="/" extends="struts-default">
            <action name="personAction_*" method="{1}" class="personAction">      
            </action>
        </package>
    </struts>

14、写web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>s2sh_integration</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 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>/*</url-pattern>
    </filter-mapping>
    <!-- spring容器和配置文件路径 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
</web-app>

15、启动项目,测试即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值