ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址
首先需要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下载地址的更多相关文章
- Junit手动/自动加载spring配置文件
分配置文件在classpath下和web-inf下两种情况的加载: ApplicationContext context = new FileSystemXmlApplicationContext(& ...
- 在web.xml正确加载spring配置文件的方式
ssm框架整合时一直报出没有创建实例bean的错误,一直以为是代码原因,反复测试了很久,才找到原因是spring配置文件没有正确导入,下图是我的错误示例 web.xml加载spring配置文件的方式主 ...
- 【Spring】Junit加载Spring容器作单元测试
如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 & ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...
- Web.xml配置详解之context-param (加载spring的xml,然后初始化bean看的)
http://www.cnblogs.com/goody9807/p/4227296.html(很不错啊) 容器先加载spring的xml,然后初始化bean时,会为bean赋值,包括里面的占位符
- struts加载spring
为了在Struts中加载Spring context,需要在struts-config.xml文件中加入如下部分: <struts-config> <plug-in classNam ...
- Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)
最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...
- Spring boot 国际化自动加载资源文件问题
Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...
- Spring Web项目spring配置文件随服务器启动时自动加载
前言:其实配置文件不随服务器启动时加载也是可以的,但是这样操作的话,每次获取相应对象,就会去读取一次配置文件,从而降低程序的效率,而Spring中已经为我们提供了监听器,可监听服务器是否启动,然后在启 ...
随机推荐
- qscoj#19D(单调队列)
题目链接:http://qscoj.cn/problem/130/ 题意:中文题诶- 思路:直接用单调栈搞一下就好了 代码: #include <bits/stdc++.h> using ...
- 联盟链FISCO BCOS权限控制一览
FISCO BCOS是完全开源的联盟区块链底层技术平台,由金融区块链合作联盟(深圳)(简称金链盟)成立开源工作组通力打造.开源工作组成员包括博彦科技.华为.深证通.神州数码.四方精创.腾讯.微众银行. ...
- .bat 文件学习
参考文章:http://www.cnblogs.com/glaivelee/archive/2009/10/07/1578737.html 重点: @echo off 关闭回显,不显示脚本中的命令 e ...
- [软件工程基础]Alpha 阶段事后分析
设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 帮助选修物理实验的学生撰写实验报告,计算实验数据,验证计算结果,并提供一个讨论的平台. 全体成员认 ...
- Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
链接:https://codeforces.com/contest/1166/problem/C 题意: The legend of the foundation of Vectorland talk ...
- HDU-1498-50years,50colors(最大匹配, 枚举)
链接:https://vjudge.net/problem/HDU-1498#author=634579757 题意: 撞气球游戏,一个n*n的矩阵中,有不同颜色的气球,气球的颜色最多50种(从1到5 ...
- 开发中mybatis的一些常见问题记录
一.oracle数据库通过mybatis的批量插入的两种方式 方式1 insert into table_tmp (id,v1,v2,v3,v4) SELECT A.*,OSM_VIID_DEVICE ...
- Java Excel API的使用
https://wenku.baidu.com/view/724cc9e2dd88d0d232d46a1b.html
- C#高级语法
委托 委托就是指针函数,委托的定义与类的属性定义类似都必须在类的方法体进行. 委托的定义: class Program { //定义委托:委托不能在方法体内定义. public delegate st ...
- I/O————对象流
对象流指的是可以直接把一个对象以流的形式传输给其他的介质,比如硬盘 一个对象以流的形式进行传输,叫做序列化. 该对象所对应的类,必须是实现Serializable接口 对象的序列化与反序列化就是从文件 ...