一、整合中遇到的问题
1.struts与spring整合要用到struts2-spring-plugin包,它作用就是不让struts自己创建Action,而是让spring创建管理Action对象。
但是我在加了这个Jar包后,发现它自带了低版本的spring包,使用exclusion标签排除了问题Jar包,下面链接是具体操作:
https://blog.youkuaiyun.com/weixin_38943098/article/details/88894078
2.struts的Action默认多例的,所以Action对象交给spring创建管理后也需要配置:scope="prototype"
【扩展】跟scope="prototype"相关的一个知识,这儿做个记录,网址:https://www.cnblogs.com/happyflyingpig/p/8047441.html
3.struts.xml文件,首先命名必须是struts.xml,且必须在src目录下。
4.struts2默认Action类是ActionSupport,默认方法是excute();所以我们创建Action一般要继承Action接口或Action类的实现类ActionSupport类。当我们的方法是:public string 方法名(); 可以不使用execute()方法,但是需要注意:访问修饰符必须public,返回值必须是String,且不能有参数。
二、整合开始
1.web.xml文件中配置spring和struts2前端控制器
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!--struts2核心控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2. applicationContext.xml配置Action
<!-- spring配置Action -->
<bean id="testAction" class="cn.jindou.crm.struts.action.TestAction" scope="prototype">
<property name="testService" ref="testService"></property>
</bean>
<bean id="testService" class="cn.jindou.crm.struts.service.impl.TestServiceImpl"/>
3.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 : key value : 值 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 修改Struts2默认拦截的URL后缀为.com -->
<constant name="struts.action.extension" value="com"></constant>
<!-- package标签:
name:包的名称 (随意,但必须唯一)
extends:指定当前包的父包(子包自动具备父包定义的内容;我们的包一般都要继承struts-default 包,因为此包具备Struts2的核心功能;struts-default 包在struts-default.xml中定义着)
abstruct:把当前包声明为抽象包(用于继承),里面一般定义着公共配置,只有没有action标签的包才能被声明为抽象包
namespqce:指定当前包的访问名称空间(URL模块化管理)名称空间的写法:必须以'/'开头,后面随意(支持多级)
-->
<package name="strutsTest" namespace="/" extends="struts-default">
<action name="testAction" method="testStrutsDemo" class="testAction">
<!-- struts既可以跳转到Jsp前端页面,也可以直接跳转到一个Action中 -->
<result name="success" type="dispatcher">/jsp/success.jsp</result>
</action>
</package>
</struts>
4. Action类:TestAction
public class TestAction extends ActionSupport implements ModelDriven<User>{
private static final long serialVersionUID = 5374565006845097892L;
// 1.模型驱动:必须实现ModelDriven接口,自行实例化对象,并实现抽象方法(返回值就是我们实例化的对象)
// User实体类中提供两个属性:与属性驱动一样的name和password
private User user = new User();
// 2.属性驱动:提供跟表单属性一致的set()和get()方法
private String name;
private String password;
//提供set方法
private TestService testService;
public String testStrutsDemo(){
//注掉的是属性驱动
/*String string = testService.testStrutsDemo(name,password);*/
String string = testService.testStrutsDemo(user.getName(),user.getPassword());
ActionContext context = ActionContext.getContext();
Map<String, Object> session = context.getSession();
session.put("str", string);
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
@Override
public User getModel() {
return user;
}
}
5. 输入url进行测试