添加struts2类库。增加 struts2-spring-plugin-x-x-x.jar文件。 "-//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中. 省了大量配置代码,不足是降低依赖关系的透明性和清晰性. |