Spring与Struts2 的整合使用

项目结构

  

再Struts2 中(还没有与Spring整合时),它创建Action类的依据

<action name="second" class="com.SecondController">
  <result name="success">/index.jsp</result>
</action>

同时还有一个点需要注意的就是,struts会每次请求时自动实例化一个新的Action类的对象

  导进相关的依赖包:pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com</groupId>
<artifactId>spring-web-struts2</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打成war包-->
<packaging>war</packaging> <dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.12</version>
</dependency> <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

struts2 与spring整合的步骤

  1:配置监听器ContextLoaderListener,以便创建出Spring的容器对象web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--配置监听器ContextLoaderListener,以便创建出Spring的容器对象-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--访问的配置路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationCtx.xml</param-value>
</context-param> <!--struts2过滤器-->
<filter>
<filter-name>sss</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sss</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

  2:添加struts-spring的依赖,以便改变默认的Struts的实例化Action类规则(上面已配置)

  1. 因为读取到的值就是bean的名字

  2. 利用WebApplicationContextUtils的方法就可以实例化action类

  3. 实例化action类的时候,就可以注入依赖的Service类型

     <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>

  创建UserDao接口

 package dao;

 public interface UserDao {
void insert();
}

  实现UserDao:UserDaoImpl

 package dao;

 public class UserDaoImpl implements UserDao {
public void insert() {
System.out.println("dao insert----");
}
}

  创建UserService接口

 package service;

 public interface UserService {
void insert();
}

  UserServiceImpl

 package service;

 import dao.UserDao;

 public class UserServiceImpl implements UserService {
private UserDao userDao ; public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void insert() {
userDao.insert();
}
}

  把UserDaoImpl和UserServiceImpl让Spring管理:applicationCtx.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDao" class="dao.UserDaoImpl"></bean>
<bean id="userService" class="service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean> <!--记得设定Action(也就是Controller)的设置,要设置为非单利,一般就设置为prototype-->
<bean id="sencondController" class="com.SecondController" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>

  配置struts并且让Spring管理

 <?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="demo" extends="struts-default">
<action name="second" class="sencondController">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>

  SecondController

 package com;

 import service.UserService;

 public class SecondController {

     public SecondController() {
System.out.println("second contructor-----");
} private UserService userService; public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
} public String execute(){
System.out.println("execute----");
userService.insert();
return "success";
} }

个人笔记,请勿喷

Spring与Struts2 的整合使用的更多相关文章

  1. 浅谈:深入理解struts2的流程已经spring和struts2的整合

    第一步:在tomcat启动的时候 1.在tomcat启动的时候,首先会加载struts2的核心过滤器StrutsPrepareAndExecuteFilter <filter> <f ...

  2. Spring与Struts2的整合

    一.复制jar文件. 把struts2-spring-plugin-..*.jar和spring.jar复制到Web工程的WEB-INF/lib目录下,并且还需要复制commons-logging.j ...

  3. spring和struts2的整合的xml代码

    导入spring的pring-framework-4.0.4.RELEASE的所有包,导入struts2下(对于初学的推荐)bin下所有的包,虽然有些包可以能现在你用不到,但可以保证你基本上不会出现缺 ...

  4. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  5. Spring框架+Struts2框架第一次整合

    1:Spring框架和Struts2框架如何整合??? Spring 负责对象创建 Struts2 用Action处理请求 2:Spring与Struts2框架整合的关键点: 让struts2框架ac ...

  6. Spring与Struts2整合VS Spring与Spring MVC整合

    Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...

  7. Spring与Struts2整合

    Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...

  8. struts2和spring的两种整合方式

    首先,来看看如何让Spring 来管理Action. 在struts.xml中加入 <constant name="struts.objectFactory" value=& ...

  9. SSH框架之Spring+Struts2+Hibernate整合篇

    回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...

随机推荐

  1. sql语句(删除重复数据只保留一条)

    用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select ...

  2. springMVC 框架的xml配置文件的说明

    springMVC框架xml文件配置的说明,直接上代码: 我们介绍四个xml文件配置以及xml内容的理解:application.xml.spring-mvc.xml.pom.xml 和 web.xm ...

  3. cenos7 下数据库相关操作

    1.Linux Centos7下如何确认MySQL服务已经启动 https://www.cnblogs.com/qianzf/p/7082484.html 2.CentOS 7上安装MySQL并配置远 ...

  4. 【转载】github 查找最火项目

    博主感觉这篇文章很有用,很方便所以转载过来学习学习. 原文链接地址https://www.cnblogs.com/poterliu/p/10634568.html 如何在github上查找star最多 ...

  5. swat - 基于web的samba管理工具

    总览 swat [ -s smb config file ] [ -a ] 描述 此程序是 samba 套件的一部分. swat 允许 samba 管理员通过web浏览器配置复杂的 smb.conf ...

  6. 总结下awk基本用法

    命令格式: awk '{commands} [{other commands}]' awk 'condition{commands} [{other commands}]' 如:awk '$4==&q ...

  7. React Native 安卓模拟器调出Dev Setting

    Android Studio 模拟器调出Dev Setting 实现热更新 cmd进入项目目录 F:\study\AwesomeProject> 执行 adb shell input keyev ...

  8. MySQL不支持事务处理的解决方法

    MySQL数据库默认的存储引擎类型是MyISAM,这种存储引擎类型不支持事务处理. 在MySQL中,只有InnoDB存储引擎类型的数据表才能支持事务处理. 因此,如果想让MySQL支持事务处理,只要将 ...

  9. Java数据库事务四大特性以及隔离级别

    四大特性ACID 原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚.失败回滚的操作事务,将不能对数据库有任何影响 一致性(Consistency) 一致性是指事 ...

  10. forms 组件的功能和使用

    forms组件 先自己实现注册功能,并且对用户输入的信息加限制条件 如果用户输入的信息不符合条件,前端展示报错信息 注册示例: 1.前端渲染标签获取用户输入 >>> 前端渲染标签 2 ...