整合struts+spring
1.拷贝struts工程
2. spring-framework-2.0-with-dependencies\dist\spring.jar+
spring-framework-2.0-with-dependencies\lib\jakarta-commons\commons-logging.jar+
spring-framework-2.0-with-dependencies\lib\log4j\log4j-1.2.14.jar+
\struts-2.3.4-all\lib\struts2-spring-plugin-2.3.4.jar
到工程/WEB-INF/lib/目录下
3.
拷贝spring-framework-2.0-with-dependencies\samples\jpetstore\war\WEB-INF\applicationContext.xml文件到工程src目录下(并且修改)。
Eg:
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="" class="">
</bean>
</beans>
3.拷贝spring-framework-2.0-with-dependencies\samples\jpetstore\war\WEB-INF\log4j.properties到src目录下。
4. 拷贝spring-framework-2.0-with-dependencies\samples\jpetstore\war\WEB-INF\web.xml中的两块代码并修改为:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
复制到WebRoot\WEB-INF\web.xml
5.新建action实现代码和service层 和dao层代码
6.完成依赖关系配置
applicationContext.xml
eg:
<bean id="myuserdao"class="com.etc.dao.impl.UserDaoImpl"/><!-- 注册dao层实现类 叫myuserdao-->
<bean id="myservice"class="com.etc.service.impl.UserServiceImpl"><!-- 注册service层实现类叫myservice需要IUserDao类型的属性 -->
<property name="userdao"ref="myuserdao"></property><!-- 将myuserdao注入 UserServiceImpl类中的userdao变量中-->
</bean>
<bean id="loginaction"class="com.etc.web.LoginAction"><!-- 注册action类叫loginaction,需要IUserService类型的属性-->
<property name="userservice"ref="myservice"></property><!-- 将myservice注入LoginAction类中的userservice变量中-->
</bean>
struts.xml
eg:
<struts>
<constant name="struts.enable.DynamicMethodInvocation"value="true"></constant><!-- 动态方法调用 -->
<constant name="struts.i18n.encoding"value="UTF-8"></constant><!-- 转码 -->
<constant name="struts.devMode"value="true"></constant><!-- 开发模式:修改struts.xml不用重启服务器 -->
<constant name="struts.objectFactory"value="spring"></constant><!-- 加入常量配置 struts action类实例由spring创建 -->
<package name="pk1" namespace="/"extends="struts-default" >
<action name="login" class="loginaction"><!-- class属性必须指定到applicationContext.xml注册action类的id属性 -->
<result name="success">/pages/login-success.jsp</result>
<result name="error">/pages/login-error.jsp</result>
</action>
</package>
</struts>