一、搭建Struts2
具体步骤可以参考Struts2官网 如何创建Struts 2 Web应用程序:
1、在maven的pom.xml添加struts2-core的依赖节点
[/span>dependency
"color: rgba(0, 0, 255, 1)"] [/span>groupId
[/span>artifactId
[/span>version
2、添加日志记录
log4j2.xml在src/main/resources包含以下内容的文件夹中设置配置
<?xml version="1.0" encoding="UTF-8"?>
[/span>Configuration
[/span>Appenders
[/span>Console name="STDOUT" target="SYSTEM_OUT"
[/span>PatternLayout pattern="%d %-5p 【%t】 %C{2} (%F:%L) - %m%n"/>
[/span>Loggers
[/span>Logger name="com.opensymphony.xwork2" level="debug"/>
[/span>Logger name="org.apache.struts2" level="debug"/>
[/span>Root level="warn"
[/span>AppenderRef ref="STDOUT"/>
3、添加log4j依赖节点
[/span>dependency
[/span>groupId
[/span>artifactId
[/span>version
[/span>dependency
[/span>groupId
[/span>artifactId
[/span>version
4、添加Struts2 servlet过滤器
在web.xml中添加过滤器
<?xml version="1.0" encoding="UTF-8"?>
[/span>web-app id="WebApp_ID" version="2.4"
xmlns=""
xmlns:xsi=""
xsi:schemaLocation=" "
[/span>display-name
[/span>welcome-file-list
[/span>welcome-file
[/span>filter
[/span>filter-name
[/span>filter-class
[/span>filter-mapping
[/span>filter-name
[/span>url-pattern
5、创建struts.xml核心配置文件,放在src/main/resources文件夹中
在struts.xml文件中我们开启动态方法调用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
""
[/span>struts
[/span>constant name="struts.devMode" value="true" />
[/span>constant name="struts.enable.DynamicMethodInvocation" value="true"/>
[/span>package name="basicstruts2" extends="struts-default" strict-method-invocation="false"
[/span>action name="index"
[/span>result
这样我们的struts2就搭建好了
6、Struts2的hello world 编写测试类
struts.xml中配置action
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
""
[/span>struts
[/span>constant name="struts.devMode" value="true" />
[/span>constant name="struts.enable.DynamicMethodInvocation" value="true"/>
[/span>package name="basicstruts2" extends="struts-default" strict-method-invocation="false"
[/span>action name="index"
[/span>result
[/span>action name="test" class="com.blb.action.TestAction"
[/span>result name="success"
编写测试类继承ActionSupport
package com.blb.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
public String hello(){
return SUCCESS;
}
}
运行项目,访问地址: localhost:8080/项目名/test!hello
这样Struts2的环境就搭建完成了
二、搭建Spring
1、在项目中添加spring-context依赖节点
[/span>dependency
[/span>groupId
[/span>artifactId
[/span>version
因为是web项目,我们还需要导入spring-webmvc依赖
[/span>dependency
[/span>groupId
[/span>artifactId
[/span>version
2、创建applicationContext.xml核心配置文件,放在src/main/resources文件夹中
<?xml version="1.0" encoding="UTF-8"?>
[/span>beans xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
"
3、在web.xml文件中添加spring监听,使spring在程序运行时启动
[/span>context-param
[/span>param-name
[/span>param-value
[/span>listener
[/span>listener-class
这样我们的spring就搭建完成了
三、整合Struts2+Spring
1、在项目中添加struts2-spring-plugin依赖节点
[/span>dependency
[/span>groupId
style="color: rgba(0, 0, 255, 1)"]org.apache.struts [/span>artifactId
[/span>version
由于struts2-spring-plugin依赖包含spring-context,spring-web,struts2-core依赖,所以我们需要把这三个依赖移除
2、在applicationContext.xml文件中注入我们的TestAction类
<?xml version="1.0" encoding="UTF-8"?>
[/span>beans xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
"
[/