===============================================struts和spring框架结合配置=============================================
要将Spring作为Struts的一个插件来使用,在struts-config.xml文件中加入如下配置:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
在Web.xml中通过下面的方式加载Spring
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
===============================================struts和spring结合三种方式============================================
1)。继承org.springframework.web.struts.ActionSupport类
将Action类的继承方式更改为org.springframework.web.struts.ActionSupport或org.springframework.web.struts.DispatchActionSupport
在Action类中需要得到Spring Context ApplicationContext context = this.getWebApplicationContext();
2)。控制器方式
需要在struts-config.xml文件中加入:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>
在applicationContext.xml文件中不仅需要注入DAO实现类,还要通过Action类的path路径注入Action类
3)。委托代理
在struts-config.xml文件中更改Action标签里的type属性为: org.springframework.web.struts.DelegatinActionProxy
在applicationContext.xml文件中与控制器方式一样,注入DAO实现类与Action。
<!– 注入实现类 -->
<bean name="threeifc" class="com.soft.test.TestIMP">
<property name="template" ref="template"></property>
</bean>
<!– 注入Action -->
<bean name="/path路径" class="com.soft.three.ThreeAction">
<property name="threeifc" ref="threeifc"></property>
</bean>
===============================================hibernate框架与spring框架结合========================================
//hibernate配置文件存放在src下面
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
=================================================jdbc数据源配置=====================================================
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:name</value>
</property>
<property name="username">
<value>username</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
==================================================JNDI数据源配置====================================================
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/oracle</value>
</property>
</bean>
<!– Hibernate的一些参数的配置 -->
<property name="hibernateProperties">
<props>
<prop key=“hibernate.dialect”><!– 数据库方言 -->
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key=“hibernate.show_sql”><!– 在控制台打印sql语句 -->
true
</prop>
</props>
</property>
<!– 表映射 -->
<property name="mappingResources">
<list>
<value>com/webssh/TableName.hbm.xml</value>
</list>
</property>
=====================================================hibernate模板注入==============================================
<!-- HibernateTemplate -->
<bean name=" hibernateTemplate " class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
使用HibernateTemplate的方式有两种:
方式一:
<!-- 将模板注入给接口,需要在实现类中写属性 -->
<bean id="classIFC" class="com.webssh.classinfo.ClassIMP2">
<property name="template" ref="hibernateTemplate"></property>
</bean>
方式二:
<!-- 将模板注入给接口,不需要在实现类中写属性 -->
接口要继承org.springframework.orm.hibernate3.support.HibernateDaoSupport类
<bean id="classIFC" class="com.webssh.classinfo.ClassIMP2">
<property name="hibernateTemplate" ref="hibernateTemplate">
</property>
</bean>

浙公网安备 33010602011771号