spring+struts2+hibernate整合

web.xml需要配置

 

复制代码
   <context-param>
        <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>
    
    <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>
复制代码

 

 

db.properties

复制代码
jdbc.user=root
jdbc.password=tiger
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///ssh

jdbc.initPoolSize=5
jdbc.maxPoolSize=10
复制代码

 

applicationContext.xml

复制代码
<context:property-placeholder location="classpath:db.properties" />
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:com/my/ssh/entities/*.hbm.xml"></property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="isValidate" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    
    <aop:config>
        <aop:pointcut expression="execution(* com.my.ssh.service.*.*(..))" id="pointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
    </aop:config>
复制代码

 

applicationContext-beans.xml

  配置各种bean  注意:对于Struts2 的Action类  scope 需要配置为prototype (不为单例)

<bean id="employeeAction" class="com.my.ssh.actions.EmployeeAction" scope="prototype">
  <property name="employeeService" ref="employeeService"></property>
  <property name="departmentService" ref="departmentService"></property>
</bean>

 

 hibernate.cfg.xml

复制代码
<hibernate-configuration>
    <session-factory>
    <!-- 配置基本信息-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>


        <!-- 二级缓存 -->
        
    </session-factory>
</hibernate-configuration>
复制代码

 

 

struts.xml

<!-- 只需要把class 配置为 在applicationContext-beans.xml 文件中Action所指向的id-->
<action name="emp-*" class="employeeAction" method="{1}"> </action>

转载于:https://www.cnblogs.com/Jansens520/p/6632774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSH整合示例(详情见我博客专栏)之前的博客我们总结了spring基础、spring分别整合struts2hibernate、mybatis等,今天我们来同时整合下 struts、springhibernate,也就是所谓的 ssh 。 整合流程: 1 首先整合springhibernate,这次我们在spring 中配置bean使用注解的方式 ,hibernate实体映射关系也使用注解的方式,配置完毕后用简单方法测试下hibernate是否整合成功。 a 加入支持:添加 spring核心包、hibernate 3.6 包、 spring整合hibernate包 , 在src下建立applicationContext.xml (先建立src下便于测试hibernate)。 b 编写实体类,加入hibernate注解,编写方法类测试类,在applicationContext.xml中添加hibernate模板类配置以及包扫描语句 。在类中添加spring bean注解。 c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立struts.xml,复制头文件进去。将applicationContext.xml移到WEB-INF目录下。web容器中(web.xml)中添加struts2 filter以及spring 监听器。 b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ioc注解的方式, 所以默认的bean的id 为 类名(首字母小写) c 编写action类、页面进行测试

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值