1.下载spring2.x,拷贝spring2.x/dist/spring.jar,struts2/lib/struts2-spring-plugin.jar到WebRoot/WEB-INF/lib
2.在src下添加spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
</beans>
3.在web.xml中添加spring的配置信息
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
4.spring配置完了,测试一下是否成功
修改类TestStruts2.java
package testJ2EE.test;
public class TestStruts2 {
public void init(){
System.out.println("init TestStruts2.");
}
public String test(){
return "success";
}
}
修改applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="testStruts2" class="testJ2EE.test.TestStruts2" scope="request" init-method="init">
</bean>
</beans>
修改struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="testStruts2" class="testStruts2" method="test">
<result name="success">/pages/test_struts2.jsp</result>
</action>
</package>
</struts>
启动tomcat,浏览器中输入http://localhost:8081/testJ2EE/testStruts2.action
不出错就成功!