`

spring的整合

阅读更多

spring和hibernate的整合

  在spring的配置文件里加入如下配置:

<!-- 配置sessionFactory -->

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

              <property name="configLocation">

<!—classpath:是spring中虚拟路径的表示方式à

                     <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>

 

spring和struts的集合

 

1、 原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询

 2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来

 

3、在struts-config.xml文件中配置Action

      * <action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy

       DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值到IoC容器中取得本次请求对应的Action

       

4、在spring配置文件中需要定义struts的Action,如:

       <bean  name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">

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

       </bean>

       * 必须使用name属性,name属性值必须和struts-config.xml文件中<action>标签的path属性值一致

       * 必须注入业务逻辑对象

       * 建议将scope设置为prototype,这样就避免了struts Action的线程安全问题

 

struts+hibernate+spring整合

在原有的配置文件基础上,在web.xml中加入一下过滤器  

 

 <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>

第一个是关于编码的过滤器,第二个是关于session的过滤器

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics