天天看点

Struts2 Spring Hibernate整合关键配置

[code]

1.在web.xml文件里面配置

<!-- Struts2 与 Spring 整合 -->

<context-param>

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

<param-value>

/WEB-INF/classes/applicationContext*.xml

</param-value>

</context-param>

<!-- 整合Struts2 -->

<filter>

<filter-name>Struts2.0</filter-name>

<filter-class>

org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

</filter>

<filter-mapping>

<filter-name>Struts2.0</filter-name>

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

</filter-mapping>

<!-- 整合Spring与Struts2的类自动加载器 -->

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

2.接下来就是在Spring的applicationContext.xml文件里面配置与hibernate相关的配置即可。

比如说:

<bean id="sessionFactory"

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

<property name="configLocation"

value="classpath:hibernate.cfg.xml">

</property>

</bean>

<!-- 事务处理 -->

<bean name="HibernateTransactionManager"

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

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

</bean>

<tx:annotation-driven transaction-manager="HibernateTransactionManager"/>

当然也可以不用hibernate.cfg.xml文件的,就是在Spring里面配置就可以了。

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="mappingResources">

<list>

<value>Widget.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>

</props>

</property>

<property name="dataSource">

<ref bean="dataSource"/>

</property>

</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>

<property name="url"><value>jdbc:hsqldb:mem:widgets</value></property>

<property name="username"><value>sa</value></property>

<property name="password"><value></value></property>

</bean>

[/code]