Struts2 与 Spring整合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangqing84411433/article/details/89376052

Struts2 与 Spring整合

利用Spring 的 IOC/AOP 功能可以很方便的管理软件的业务层和控制器. Struts2 提供了 Struts2-spring-plugin插件, 可以自动的将Struts2 和Spring进行整合, 整合之后Struts2可以利用Spring作为工厂, 生产Struts2控制器实例

整合的作用

1.利用Spring管理Action对象

2.Spring管理Action的同时还可以为控制器注入业务层

最终目的:

将控制器与业务层整合

步骤:

1.导包,除了struts2的配置文件外还需要一个插件包,版本与struts2一致,本案例导包如下:

<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>cn.tedu</groupId>
  <artifactId>struts_day02</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
  	<!-- struts2所需jar包 -->
  	<dependency>
  		<groupId>org.apache.struts</groupId>
  		<artifactId>struts2-core</artifactId>
  		<version>2.3.24</version>
  	</dependency>
  	
  	<!-- struts2 整合spring所需jar包 且与 struts2-core 版本保持一致-->
  	<dependency>
	    <groupId>org.apache.struts</groupId>
	    <artifactId>struts2-spring-plugin</artifactId>
	    <version>2.3.24</version>
	</dependency>
  	
  </dependencies>
</project>

2.配置文件(两个),一个为struts.xml已存在,再添加一个spring的配置文件,用spring的模板文件即可,注意此配置文件中的版本要与导入包的spring配置文件一致,导包的spring版本是3.24,配置文件中显示大致如下:

<?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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- spring的组件扫描 -->
	<context:component-scan base-package="cn.tedu.web"/>
	
</beans>

要开启spring的组件扫描

3.web.xml中配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>struts_day02</display-name>
  
  <filter>
    <display-name>StrutsPrepareAndExecuteFilter</display-name>
    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置监听器, 在Web服务器启动时候初始化Spring容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:conf/spring-*.xml</param-value>
  </context-param>
  
</web-app>

项目结构如下:

案例测试时配置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 namespace="/login" name="login" extends="struts-default">

		<!-- 
			使用Spring管理 Action组件 
			在配置Action期间,将class属性引用Bean的ID即可,不用类名
		-->
		<action name="web" class="webAction">
			<result name="success">/WEB-INF/success.jsp</result>
		</action>
		
	</package>
</struts>

Action如下:

package cn.tedu.web;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.tedu.action.BaseAction;

@Controller
@Scope("prototype") //限制安全性问题  一个请求会创建一个action
public class WebAction extends BaseAction{
	
	public String execute(){
		System.out.println("Web Actiion");
		request.put("message", "I am from Spring!");
		return SUCCESS;
	}
}

URL:http://localhost:8000/struts_day02/login/web.action

控制台输出:Web Actiion

jsp页面输出:I am from Spring!

猜你喜欢

转载自blog.csdn.net/wangqing84411433/article/details/89376052