首先需要JAR包

Spring整合Structs2的JAR包

struts2-spring-plugin-2.3.4.1.jar

下载地址

链接: https://pan.baidu.com/s/1o7I0Bdo 密码: eg3a

spring-web-4.2.4.RELEASE.jar

这个JAR包在Spring框架包的libs中有


Structs2所需JAR包如下:

也需要放进来

我整理的Struts2下载地址

链接: https://pan.baidu.com/s/1mh9blwc 密码: nsrp


制作一个Web项目,当服务器启动时自动加载Spring中的XML配置文件 启动struts过滤

web.xml文件代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>2017-12-30_SSH</display-name> <!-- 监听的文件名 ContextLoaderListener父类ContextLoader中的一个属性得到param-name-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param> <!-- 服务器启动自动加载XML配置文件 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 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> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

注意struts2的配置文件struts.xml文件名和位置不要弄错,放在src路径下

因为src下是类路径

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>
<package name="default" extends="struts-default" namespace="/"> <!-- action的class不要写全名会创建两个对象,而写Spring配置文件中id的内容,只建一个对象
前提有struts2-spring-plugin-2.3.4.1.jar --> <action name="userAction" class="userAction">
</action>
</package>
</struts>

这里的<action name="userAction" class="userAction">

class无需再写类的全名,否则对象和Spring的bean.xml中对象相重,同时建立两个对象了

bean.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- c3p0连接池得到dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <bean id="userAction" class="com.swift.action.UserAction" scope="prototype"></bean> </beans>

这里的<bean id="userAction" class="com.swift.action.UserAction" scope="prototype"></bean>

Scope中的prototype是非单例对象

action对象的类要继承ActionSupport类

代码如下:

package com.swift.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

    @Override
public String execute() throws Exception {
System.out.println("action..................");
return NONE;
} }

显示结果如上

ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址的更多相关文章

  1. Junit手动/自动加载spring配置文件

    分配置文件在classpath下和web-inf下两种情况的加载: ApplicationContext context = new FileSystemXmlApplicationContext(& ...

  2. 在web.xml正确加载spring配置文件的方式

    ssm框架整合时一直报出没有创建实例bean的错误,一直以为是代码原因,反复测试了很久,才找到原因是spring配置文件没有正确导入,下图是我的错误示例 web.xml加载spring配置文件的方式主 ...

  3. 【Spring】Junit加载Spring容器作单元测试

    如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 & ...

  4. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...

  5. Web.xml配置详解之context-param (加载spring的xml,然后初始化bean看的)

    http://www.cnblogs.com/goody9807/p/4227296.html(很不错啊) 容器先加载spring的xml,然后初始化bean时,会为bean赋值,包括里面的占位符

  6. struts加载spring

    为了在Struts中加载Spring context,需要在struts-config.xml文件中加入如下部分: <struts-config> <plug-in classNam ...

  7. Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)

    最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...

  8. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

  9. Spring Web项目spring配置文件随服务器启动时自动加载

    前言:其实配置文件不随服务器启动时加载也是可以的,但是这样操作的话,每次获取相应对象,就会去读取一次配置文件,从而降低程序的效率,而Spring中已经为我们提供了监听器,可监听服务器是否启动,然后在启 ...

随机推荐

  1. 剑指Offer的学习笔记(C#篇)-- 反转链表

    题目描述 输入一个链表,反转链表后,输出新链表的表头. 一 . 概念普及 关于线性表等相关概念请点击这里. 二 . 实现方法 目前,可以有两种方法实现该要求. 方法一:借助外部空间实现.这里可以将单链 ...

  2. java 多线程下载文件并实时计算下载百分比(断点续传)

    多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...

  3. 36小时极客嘉年华!FISCO BCOS黑客马拉松报名启动

    FISCO BCOS是完全开源的联盟区块链底层技术平台,由金融区块链合作联盟(深圳)(简称金链盟)成立开源工作组通力打造.开源工作组成员包括博彦科技.华为.深证通.神州数码.四方精创.腾讯.微众银行. ...

  4. IDEAL基于maven创建spark程序

    今天创建spark项目遇到一个奇葩问题,困扰了好久,特此记录一下. 1.按照截图创建spark项目 2.项目创建好后,运行报错: Error:scalac: error while loading J ...

  5. layui时间控件联动:开始时间、结束时间,有效时间范围

    实际开发中,经常遇到时间控件联动的情况,然后每次都网上搜代码Copy,不过每次都有点小Bug让你错不及防: 这次,在这里备份一下,以后Copy自己的得了(欢迎Copy代码,若遇到问题,请麻烦回复我一下 ...

  6. STP-14-MST配置

    在配置MST之前,工程师要进行一定程度的预先规划.首先,必须决定是否应该使用多区域设计,以及如何设置边界.多区域的设计使得每个区域都有独立的MST实例编号.VLAN到实例的映射,以及独立的实例根.整体 ...

  7. VUE中模块与组件

    组件:我们项目中,每一个资源(.js,.css,.vue,...)都是一个模块,这些模块是相互独立,但是我们可以通过类似于webpack构建工具把它们整合在一起,你可以理解为模块就是一个一个积木,通过 ...

  8. vue之webpack打包工具的使用

    vue之webpack打包工具的使用 一.什么是webpack? webpack是一个模块打包工具.用vue项目来举例:浏览器它是只认识js,不认识vue的.而我们写的代码后缀大多是.vue的,在每个 ...

  9. 送气球.jpg(模拟)

    链接:https://ac.nowcoder.com/acm/contest/318/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  10. Linux systemd 常用命令

    系统管理 systemctl 显示系统状态: $ systemctl status 立即激活单元: # systemctl start [单元] 立即停止单元: # systemctl stop [单 ...