struts2和Spring整合配置文件_百步-孙俊财_百度空间

添加struts2类库。增加 struts2-spring-plugin-x-x-x.jar文件。
在web.xml中添加Spring上下文参数和xxx。(在struts2的过滤器之前)。
<context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>     
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-------------------------------------------------------------
在web.xml中添加lazyLoadingFilter过滤器。要放在struts2过滤器之前。但过滤器映射集中到所有过滤器之后。
页面显示完了才关闭session。
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class> </filter>
<filter-mapping>
   <filter-name>lazyLoadingFilter</filter-name>
   <url-pattern>*.action</url-pattern>
</filter-mapping>
------------------------------------------------------------------
在web.xml中添加struts2所需的过滤器。
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
=================================
在struts.xml配置文件,添加constant,将action交由Spring管理。
<struts>
       <constant name="struts.objectFactory" value="spring" />
</struts>
-----------------------------------------
添加action配置。(action类,继承com.opensynphony.xwork2.ActionSupport)
注意这些action,并不指定类,而是指定了代号(bean的id),如class="addBean"。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC

       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
       "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
       <include file="struts-default.xml"/> (似乎并没有此句)
     
           <package name="crm_employee" extends="struts-default" namespace="/emp">
           <action name="add" class="addBean" method="add">
               <result>list.action</result>
           </action>
           <action name="list" class="listBean" method="list">
               <result>/emp/list.jsp</result>
           </action>
           <action name="delete" class="deleteBean" method="delete">
               <result>list.action</result>
           </action>
       </package>
</struts>
==================================
在applicationContext.xml配置action。
           <bean id="addBean" class="com.liuwei.crm.action.EmployeeAction" scope="prototype">
               <property name="employeeManager>
                        <ref bean="employeeManager" />
               </property>
           </bean>
           <bean id="deleteBean" class="com.liuwei.crm.action.EmployeeAction" scope="prototype">
               <property name="employeeManager>
                        <ref bean="employeeManager" />
               </property>
           </bean>
           <bean id="listBean" class="com.liuwei.crm.action.EmployeeAction" scope="prototype">
               <property name="employeeManager>
                        <ref bean="employeeManager" />
               </property>
           </bean>
---------------------------------------------------
添加事务管理。先修改开头。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
配置事务管理器,事务传播特性。。
        <!-- 配置事务管理器 -->
        <bean id="transactionManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory">
                        <ref local="sessionFactory" />
                </property>
        </bean>
        <!-- 配置事务特性 ,配置 add、delete 和 update 开始的方法,事务传播特性为required-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
                <tx:attributes>
                        <tx:method name="add*" propagation="REQUIRED" />
                        <tx:method name="delete*" propagation="REQUIRED" />
                        <tx:method name="update*" propagation="REQUIRED" />
                        <tx:method name="*" read-only="true" />
                </tx:attributes>
        </tx:advice>
        <!-- 配置哪些类的方法进行事务管理, 当前 cn.com.jobedu.crm.service 包中的子包、 类中所有方法需要,还需要参考 tx:advice 的设置 -->
        <aop:config>
                <aop:pointcut id="allManagerMethod"
                        expression="execution (* cn.com.jobedu.crm.service.*.*(..))" />
                <aop:advisor advice-ref="txAdvice"
                        pointcut-ref="allManagerMethod" />
        </aop:config>
===============================
编写页面。/emp/list.jsp
<% taglib uri="/struts-tags" prefix="s"%>
输出信息。
<s:iterator value="employees" >
<s:property value="id" />
<s:property value="address" />
</s:iterator>
===================================
调试中出错,包冲突,去掉了hibernate3.1核心库中的一个jar。(xercex-2.6.2.jar)(必须在项目能力那里去除,否则重新部署又会添加)。
===================================================
几种方式整合struts2和Spring -struts2xx指南 -hxzon
   
1.初始化Spring容器(两种方式,优先使用{dy}种)
一,利用ContextLoaderListener
<listener>     
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
默认查找WEB-INF下的applicationContext.xml.
如果有多个配置文件,则还需要在之前添加参数如下
<context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
二,利用load-on-startup Servlet
这种方式主要是有些web服务器不支持servlet2.3以上的规范.
<servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <!-- 以下值越小越早进行容器创建,但Listener总是比所有的servlet更早 --》
    <load-on-startup>1</load-on-startup>
</servlet>
如果有多个配置文件,也要像{dy}种方式那样添加参数。
   
2.struts2和Spring整合(hxzon:推荐使用{dy}种方式)
一,struts.xml中的修改
只需将action的class属性改为applicationContext.xml里对应Bean实例的id.
applicationContext.xml里添加action对应的Bean.
<bean id="xxx" class="实现类" scope="prototype">
    <property name="yyy" ref="yyy" />
</bean>
不足是action配置了两次,冗余.
二,使用自动装配
struts.xmlxx不需要修改.
applicationContext.xml添加业务逻辑组件.
<bean id="xxx" class="业务逻辑实现类" />
这样就可以通过匹配id名自动将xxx注入到action中.
省了大量配置代码,不足是降低依赖关系的透明性和清晰性.


郑重声明:资讯 【struts2和Spring整合配置文件_百步-孙俊财_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——