笔记-----ssh整合

一、先整合spring和Hibernate
   1.导入jar包
           添加spring的jar包(dist下所有,commons-logging,aop的三个jar)
           导入hibernate的jar包(jdbc驱动jar,核心jar,required下的所有,cglib,jpa,slf4j)
          
   2.提供spring的配置文件
          (1)spring的基本配置
                 
        annotation 4.9章
             <context:annotation-config/>
             <context:component-scan base-package="com"></context:component-scan>
        aop
             <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  
   3.配置数据库连接池(测试)
      1.提供连接池的jar包(commons-pool.jar commons-dbcp.jar)
      2.配置(文档中搜索dbcp)
    
    
   4.配置Hibernate的SessionFactory(文档中14.3)(测试)
     SessionFactory只需要创建一次,所以交给spring来帮我们创建。
     hibernate的sessionFactory没有使用数据源,所以没有setDataSouce这个方法,所以
            要spring的SessionFactory
     (1)org.springframework.orm.hibernate3.LocalSessionFactoryBean
                   这个类是xml配置文件版本的,如果hibernate使用的是注解版本,需要使用
        org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
    
     (2)如果是Annotation版本 不能使用mappingResouces,(到源代码)
         <property name="annotatedClasses">
                    用<property name="packagesToScan" value="com.wangke.po"></property>最简单
    
     (3)提供hibernate的基本配置(用hiberate的配置文件自动生成然后copy)
                 注意,属性的名字一定要加上hibernate.
                 如:<prop key="hibernate.format_sql">true</prop>
    
     (4)配置HibernateTemplate   
                  配置好的 HibernateTemplate不能用继承,setSessionFactory所有的类是抽象类
                所有只能把HibernateTemplate注入到Dao中     ]
       
              解决的方法,自己写一个类DaoSupport继承自HibernateTemplate
               手动注入SessionFactory,调用的是父类的注入方法
               super.setSessionFactory();
              
     (5)使用getCurrentSession的问题
        1.只能写在有事务的代码中
       
        2.千万不要配置<prop key="hibernate.current_session_context_class">thread</prop>
          spring默认就是从thread中取的
         
     (6)使用@Transactional注解
                  相当于给方法加上了事务的功能,和HbernateTemplate是一样的
          1.配置spring,在文档中搜索11.5.6. Using @Transactional
            <tx:annotation-driven transaction-manager="txManager"/>
 
         <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 
              <property name="sessionFactory" ref="mySessionFactory"/>
            </bean>
           
           2.@Transactional对set方法不起任何作用
          
          
       
二、整合spring和struts
   1.导入struts的jar包
  
   2.去掉重复的jar包
     commons-logging  antlr
    
   3.提供struts—confifg.xml放到WEB-INF下
  
   4.在web.xml中配置ActionServlet
  
   5.(1)监听器
      spring创建的对象都必须使用ApplicationContext对象的getBean()方法
     tomcat启动的时候就读取spring配置文件创建ApplicationContext对象
            利用ServletContext的监听器监听ServletContext的创建(服务器启动),
            读取spring配置文件创建ApplicationContext对象放到application中去
        
      spring帮我们写好了这个监听器  
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      @Rsource(name="sessionFactory")
              监听器需要知道你spring配置文件的名称
              在web.xml配置文件中 application.setAttribute();
        <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath*:applicationContext_*.xml /WEB-INF/applicationContext_*.xml</param-value>
        </context-param>
       
     (2)struts的插件
    
       <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
        <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext_*.xml"/>
       </plug-in>
    
    
          第一种整合方案:
                服务器启动的时候 读取spring配置文件创建ApplicationContext对象放到application中去
                 在action中要获取service的对象就需要把application中的ApplicationContext拿出来调用
       getBean();
      ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
     
       第二种方案
       注入的方式;不能够直接在action中注入service.action是由struts帮你创建的,不能交给spring
       管理,所以不能注入。   
       1.既然不能直接使用struts给我们创建的action,我们可以给action写代理类 
     <action path="/user"
            type="org.springframework.web.struts.DelegatingActionProxy"
            name="regForm"
            parameter="command">
        <forward name="success" path="/ok.jsp"></forward>
    </action>
   
       2@Controller("/user")中的name必须跟struts-config.xml中
       <action>标签中的path值一摸一样
   
    每个action中的type属性都是type="org.springframework.web.struts.DelegatingActionProxy"
    没有必要重复配置
    <controller>
     <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
    </controller>
   
     
     
 给三大框架提供额外的功能,spring提供了很多的过滤器,如果使用过滤器最后使用监听器
 1.给request和response设置统一编码
   <filter>
  <filter-name>charSet</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>charSet</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>
 
2让session在视图中保持打开,这样才能在视图层使用延迟加载的对象
    <filter>
  <filter-name>openSessionInView</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
 </filter>
 
    <filter-mapping>
  <filter-name>openSessionInView</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 openSessionInView里面需要注入sessionFactory,它里面属性的名称就叫做sessionFactory
 所以配置文件中的名字不能再叫mySessionFactory.
 

  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值