Struts1+spring+hibernate整合時幾個關鍵的類

來源:互聯網
上載者:User

一.當然是載入struts1的ActionServlet,在web.xml中配置

    <servlet>
   <servlet-name>action</servlet-name>
   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
   <init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>action</servlet-name>
   <url-pattern>*.do</url-pattern>
  </servlet-mapping>

二.是載入spring的applicationContext的類,有三種方式,分別對應三個類

    1.在web.xml中用ContextLoaderServlet載入

    <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/source-config.xml,/WEB-INF/dao-config.xml,/WEB-INF/action-config.xml</param-value>
  </context-param>
  <!--利用servlet載入,如果有多個spring設定檔,須設定全域的常量<context-param>
  否則,spring設定檔必須是applicationContext.xml
   -->
  <ervlet>
   <servlet-name>context</servlet-name>
   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
   <load-on-startup>0</load-on-startup>
  </servlet>

    2.在web.xml中用ContextLoaderListener載入

    <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/source-config.xml,/WEB-INF/dao-config.xml,/WEB-INF/action-config.xml</param-value>
  </context-param>
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

    3.在strutx.xml中用ContextLoaderPlugIn載入

    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" value="/WEB-INF/source-config.xml,/WEB-INF/dao-config.xml,/WEB-INF/action-    config.xml"/>
 </plug-in>

三.struts到spring轉寄的兩種方式,分別對應兩個類

    1.在struts.xml用DelegatinActionProxy轉寄

    <action-mappings>
  <action path="/register"
  name="registerForm" type="org.springframework.web.struts.DelegatingActionProxy"
  input="/register.jsp">
  <forward name="success" path="/success.jsp"></forward>
  </action>
 </action-mappings>

    action的path屬性對應spring設定檔bean的name屬性

    2.在struts.xml用DelegatingRequestProcessor轉寄

    <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>

    在action中不用配置type屬性

四.spring和hiberante結合時幾個類

    1.初始化資料庫連接的類,有幾種方式

    一是用spring提供的jdbc串連類DriverManagerSource,如下

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
      <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;databasename=test"/>
      <property name="username" value="sa"/>
      <property name="password" value="129127"/>
     </bean>

    二是用c3p0串連池ComboPooleDataSource,如下:

     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
      <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/chuanqi"></property>
      <property name="user" value="root"></property>
      <property name="password" value="129127"></property>
      <property name="minPoolSize" value="5"></property>
      <property name="maxPoolSize" value="40"></property>
      <property name="initialPoolSize" value="5"></property>
      <property name="maxIdleTime" value="20"></property>
     </bean>

    三是用dbcp串連池BasicDataSource,jar包為common-dbcp-XXX.jar和c3p0配置差不多,但現在已不推薦使用.

    2.SessionFactory類LocalSesssionFactoryBean

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/chuanqi/hibernate/vo/Classinfo.hbm.xml</value>
    <value>com/chuanqi/hibernate/vo/Courseinfo.hbm.xml</value>
    <value>com/chuanqi/hibernate/vo/Knowledgeinfo.hbm.xml</value>
    <value>com/chuanqi/hibernate/vo/Question.hbm.xml</value>
    <value>com/chuanqi/hibernate/vo/Userinfo.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
   </props>
  </property>
 </bean>

 3事務配置方式兩種,一種是傳統的TransactionProxyFactoryBean和HiberanteTransactionManager的結合

    <bean id="transcationManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 配置事務 -->
 <bean id="proxyTemplate"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager" ref="transcationManager"></property>
  <property name="target">
   <ref bean="business"></ref>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

另一種是用攔截器TransactionInterceptor,HibernateTransactionManager和BeanNameAutoProxyCreator三個類完成

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 
 <bean id="tranactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager"></property>
  <property name="transactionAttributes">
   <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <list>

    <!--這裡可以配置多個商務邏輯-->
    <value>userBusi</value>
   </list>
  </property>
  
  <property name="interceptorNames">
   <list>

<!--這裡可以配置多個攔截器-->
    <value>tranactionInterceptor</value>
   </list>
  </property>
 </bean>

    

以上參考李剛struts2權威指南

    

 

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.