前期准备工作:
1. 先从官网下载相关的lib文件,spring-3.2.0.M1.rar 和 struts-2.3.16-all.jar
2. 建立一个Web项目
开始配置:
1.在web.xml种添加导入Spring的相关配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Smart</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/smart/config/applicationContext.xml,classpath:com/smart/config/spring-severlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.将Spring所需要的jar包,copy到lib目录下
3. 在配置web.xml中添加struts的相关配置
<!-- struts 2 configuration -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.smart.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4. 添加struts配置所需的几个必须的文件
Struts必须的jar包只有五个,分别是
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.1.jar
xwork-2.0.4.jar
但是如果要和Spring联合使用就需要其他的几个jar包了,还有javassist.jar这个包在struts的lib中是没有的,需要在struts2-blank中找。
5. 配置struts.xml,如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="struts" extends="struts-default">
<action name="register" class="registerAction">
<result name="success">/jsp1/Login.jsp</result>
</action>
</package>
</struts>