• 添加 Spring Plug in
Ø <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
Ø <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
Ø </plug-in>
• 替换 Action 的 Type
Ø <action path="/login“ …
Ø type="org.springframework.web.struts.DelegatingActionProxy" />
• 在 Spring 配置文件中配置 Bean
Ø 要点: 通过 path 和 bean 的 name 进行匹配, 这两个值必须一样
Ø <bean name="/login" class="com.test.struts.action.LoginAction"></bean>
• 注入 DAO 的代理 bean
• 详细步骤参考文档
Spring 整合 Struts 的两种方式
• struts1加载spring的两种方式:
第一种:通过web.xml配置加载spring上下文环境,其配置方式如下:
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
通过listener加载
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
或者利用severlet类加载
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
第二种方式:
使用Struts插件
在struts-config.xml中
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>
• 这两种配置的明显区别就是,第一种方式可以配置多个spring配置文件。
Asm 出错和 log4j.properties 文件
• Log4j.properties
Ø # Configure logging for testing: optionally with log file
Ø log4j.rootLogger=WARN, stdout
Ø # log4j.rootLogger=WARN, stdout, logfile
Ø log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Ø log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
Ø log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
Ø log4j.appender.logfile=org.apache.log4j.FileAppender
Ø log4j.appender.logfile.File=target/spring.log
Ø log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
Ø log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
• 删除 asm-2.2.3.jar