SSH学习笔记之关于struts-spring-hibernate整合配置问

关于struts-spring-hibernate整合配置问题

 

 

最近没什么事儿,所以正在用ssh写了一个BBS!

发现并解决了一些问题!

 

 

SSH配置问题一直是一个很烦人的问题,我也是其中一个!曾一度被它整的崩溃!其实ssh本身就存在着问题,它们的有些包是互不兼容的,有时候你把它们都拷到一块,就会出问题!所以配置的时候首先记住一点,一定要让版本新的包替代版本旧的包!现在就来说说它们的配置!

 

   第一种是不删除hibernate.cfg.xml文件!

     首先在你的项目的WEB-INFO下找到web.xml文件,配置如下:

 

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" 

xmlns="http://java.sun.com/xml/ns/j2ee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  

  <servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

      <param-name>config</param-name>

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>2</param-value>

    </init-param>

    <init-param>

      <param-name>detail</param-name>

      <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

  </servlet>

 

 

  <!-- Standard Action Servlet Mapping -->

  <servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>  

 

   <context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath*:applicationContext-*.xml

    </param-value>

     <!-- 这儿或者这样写

    <param-value>/web-info/classes/applicationContext-*.xml

    </param-value>

      -->

  </context-param>

   

   <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

   

  <filter>

    <filter-name>Spring character encoding filter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

     <param-name>encoding</param-name>

     <param-value>GBK</param-value>

    </init-param>

  </filter>

 

  <filter-mapping>

    <filter-name>Spring character encoding filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  

  <filter>

    <filter-name>hibernateFilter</filter-name>

    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

  </filter>

 <filter-mapping>

    <filter-name>hibernateFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping> 

</web-app>

 

接着在你的spring配置文件applicationContext.xml文件中进行配置!这儿最好将applicationContext.xml分成几个三个文件:

                   applicationContext-common.xml

                   applicationContext-beans.xml

                   applicationContext-actions.xml

就算分开它读的还是一个文件,因为你在配置文件中是这样写的applicationContext-*.xml!

 

哦,这儿在配置之前把hibernate.cfg.xml也给贴出来!

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.url">jdbc:mysql://localhost/spring_struts_hibernate</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.show_sql">true</property>

<property name="hibernate.hbm2ddl.auto">update</property>

 

<mapping resource="xxx/xxx/xxx.hbm.xml"/>

</session-factory>

 

</hibernate-configuration>

 

接着进行applicationContext-common.xml的配置

 

<?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: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-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

<!-- 配置sessionFactory -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="configLocation">

<value>classpath:hibernate.cfg.xml</value>

</property>

</bean>

 

<!-- 配置事务管理器 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

 

<!-- 配置事务的传播特性-->

<tx:adviceid="txAdvice"transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="save*" propagation="REQUIRED" />

<tx:method name="delete*" propagation="REQUIRED" />

<tx:method name="*" read-only="true" />

</tx:attributes>

</tx:advice>

 

<!-- 那些类的哪些方法参与事务 -->

<aop:config>

<aop:pointcut id="allDaoMethod"

expression="execution(*com.guestbook.dao.hibernate.*.*(..))" />

<aop:advisorpointcut-ref="allDaoMethod"advice-     ref="txAdvice" />

</aop:config>

 

</beans>

 

这样spring和hibernate就算整合完毕了!

 

然后再进行applicationContext-beans.xml的配置,这个文件主要是用来配置你对数据库的管理类!

 

<?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: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-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

<bean id="xxxDao"class="">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

 

</beans>

其中你的数据库管理类要继承HibernateDaoSupport

要给HibernateDaoSupport依赖注入sessionFactory

 

最后在配置你的struts-config.xml文件!

 

<?xml version="1.0" encoding="ISO-8859-1" ?>

 

<!DOCTYPE struts-config PUBLIC

          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

 

<struts-config>

 

<form-beans>

<form-bean name="dynaForm"

type="org.apache.struts.action.DynaActionForm">

<form-property name="name" type="java.lang.String" />

 

</form-bean>

</form-beans>

 

<action-mappings>

<action path="/guestbook" 

        type="org.springframework.web.struts.DelegatingActionProxy"

        name="dynaForm"

        scope="request"

        parameter="method">

<forward name="guestbook.list" path="/index.jsp"></forward>

        </action>

</action-mappings>

<message-resources parameter="MessageResources" />

</struts-config>

 

这里的actionform我是采用动态的action的,当然你也可以不用!至于action,把type置为org.springframework.web.struts.DelegatingActionProxy

这样就 不用在文件中加上controllor和plug-in了,比较方便!当然这样,还没完!还需要在spring的配置文件中配置action

 

<?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: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-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

<bean name="/jsp/user" class="" scope="prototype">

<property name="userdao" ref="userDao"/>

</bean>

 

</bean>

 

</beans>

 

<property name="userdao" ref="userDao"/>这一行也可以不配,你在action中显示的new一个就userdao类就好了!

 

这样我们的配置就算大功告成了!

 

 

至于第二种配置方案:

 

很简单,只需在 applicationContext-common.xml改成

 

<?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: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-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName">

<value>com.mysql.jdbc.Driver</value>

</property>

<property name="url">

<value>jdbc:mysql://localhost:3306/myznt</value>

</property>

<property name="username">

<value>root</value>

</property>

<property name="password">

<value>root</value>

</property>

</bean>

 

<!-- jndi 连接池配置

<bean id="dataSource"

class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">

<value>java:comp/env/jdbc/myznt</value>

</property>

</bean>

-->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

<prop key="hibernate.connection.autocommit">true</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>com/myznt/vo/User.hbm.xml</value>

 

</list>

</property>

</bean>

 

<bean id="hibernateTemplate"

class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

 

<!-- 配置事务管理器 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

 

<!-- 配置事务的传播特性-->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="delete*" propagation="REQUIRED" />

<tx:method name="*" read-only="true" />

</tx:attributes>

</tx:advice>

 

<!-- 那些类的哪些方法参与事务 -->

<aop:config>

<aop:pointcut id="allDaoMethod"

expression="execution(* com.myznt.dao.*.*(..))" />

<aop:advisor pointcut-ref="allDaoMethod" advice-ref="txAdvice" />

</aop:config>

</beans>

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值