一.當然是載入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權威指南