1、Struts+spring的整合
·web.xml中
·加载的spring配置文件
·<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext*.xml / classpath*:applicationContext*.xml
</param-value>
</context-param>
·容器启动时加载
·<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
·struts-config.xml
·<action path="/test/ssh" type="org.springframework.web.struts.DelegatingActionProxy" parameter="command" ../>
·parameter="command" 是因为使用了DispatchAction
·contextApplication.xml
·<bean name="/test/ssh" class="org.kker.drp.sysmgr.struts.items.ItemAction" >
<property name="itemManager">
<ref bean="itemManager"/>
</property>
</bean>
*************************************************************************************************************
2、Hibernate+Spring的整合
·web.xml中
·加载spring配置文件 (如上)
·创建过滤器:
·<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
·contextApplication.xml(事务)
·引入spring-tx-2.0.xsd、spring-aop-2.0.xsd
*DI
*声明事务
·<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 bean="sessionFactory"/>
</property>
</bean>
*配置事务的传播特性
·<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
*配置AOP
·<aop:config>
<aop:pointcut id="managerMethod" expression="execution(* org.kker.drp.basedate.hibernate.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="managerMethod"/>
</aop:config>
*添加业务逻辑操作
·<bean id="userManagerDao" class="org.kker.user.UserManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
■:public class UserManager extends org.springframework.orm.hibernate3.support.HibernateDaoSupport
·具体使用:
·this.getHibernateTemplate().save(user);
·特定sql语句
@List list = this.getHibernateTemplate().executeFind()(new HibernateCallback(){
public Object doInHibernate(Session session)throw HibernateException,SQLException{
return (session.createQuery("from User u order by u.id").list());
}
});
·Other
·spring 的过滤器
·<filter>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<filter-name>Spring Character Encoding</filter-name>
<init-param>
<param-name>encoding</param-name>
<param-value>gb18030</param-value>
</init-param>
</filter>
·<filter-mapping>
<filter-name>Spring Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>