struts1.3整合spring2.5(将spring委托给struts方式)
前提是配置完struts1.3
-
导包
- spring-2.5.6.jar //spring核心包
- spring-webmvc-struts-2.5.5.jar //struts整合spring使用
- log4j-1.2.91.jar //log
-
spring配置
- struts-config.xml
<!--配置spring用的--> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> <!--配置spring 将spring委托给struts--> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/config/applicationContext*.xml" /> </plug-in>
- applicationContext.xml
<bean name="userManager" class="com.myframe.business.UserManager"></bean> <bean name="/User" class="com.myframe.struts.action.UserAction"> <property name="userManager" ref="userManager"></property> </bean>
- 创建一个UserManager类
public class UserManager { public void run(){ System.out.println("aaa"); } }
- Action中调用
//spring需要创建的对象 private UserManager userManager; public UserManager getUserManager() { return userManager; } public void setUserManager(UserManager userManager) { this.userManager = userManager; } //方法 public ActionForward addUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ userManager.run(); return null; }
-
log4j的配置
- 创建log4j.properties
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml! # For all other servers: Comment out the Log4J listener in web.xml to activate Log4J. log4j.rootLogger=info, 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.RollingFileAppender log4j.appender.logfile.File=${jadlstruts1spring2.root}/WEB-INF/myframe.log log4j.appender.logfile.MaxFileSize=100MB # Keep three backup files. log4j.appender.logfile.MaxBackupIndex=10 # Pattern to output: date priority [category] - message log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
- web.xml的配置
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>