spring3.1.1和struts2.3.3都发布了GA版本,于是下载整合到一起。
新建web工程,将spring3.1.1和struts2.3.3下载后,然后将所有的jar包放到项目中,
这些jar不是全部有用,建议删除一些,或者加上其他的jar,项目才能正常运行。
建议删除的jar:struts2-portlet*.jar,struts2-gxp*.jar,struts2-osgi*.jar,struts2-codebehind*.jar.
几个常用的文件和测试类:
1。applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>
<beans>
<bean id="testSpring" name="testSpring" class="com.xzxds.lei.test.TestSpring"/>
<bean name="testStruts2" class="com.xzxds.lei.test.TestStruts2"></bean>
</beans>
2。struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="testStruts2" class="testStruts2" method="testMethod"/>
</package>
<!--
<include file="example.xml"/> -->
<!-- Add packages here -->
</struts>
3。web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>xzxds-web-config</display-name>
<!-- struts config begin-->
<filter>
<filter-name>struts2x</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2x</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts config end-->
<!-- spring config begin-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:struts.xml</param-value>
</context-param>
<!-- spring config end-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4。测试类
package com.xzxds.lei.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction {
/**
* test struts and spring
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
TestSpring ts=(TestSpring)ac.getBean("testSpring");
System.out.println(ts.getName());
TestStruts2 ts2=(TestStruts2)ac.getBean("testStruts2");
ts2.testMethod();
}
}
package com.xzxds.lei.test;
public class TestSpring {
private String name="wooo";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.xzxds.lei.test;
import com.opensymphony.xwork2.ActionSupport;
public class TestStruts2 extends ActionSupport{
public String testMethod() throws Exception {
System.out.println("struts2 success");
return "success";
}
}