美文网首页
Spring整合Struts2

Spring整合Struts2

作者: 暮秋moco | 来源:发表于2020-02-25 12:59 被阅读0次

前提

在WEB应用中使用Spring

1. 整合的目标

使IOC容器管理Struts2中的Action

2. 步骤

要求:在web项目中用spring和struts2调用service类中的方法,并跳转到success.jsp中

  • 目录结构


    图片.png
  • 全部jar包


    图片.png
  • 创建一个module


    图片.png
  • 正常引入Spring的Jar包

  • 创建applicationContext.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"
       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">


</beans>
  • 在web.xml中初始化IOC容器
<?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">
    
    <!-- 配置Spring配置文件的名称和位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 启动IOC容器的ContextLoaderListener监听,将IOC容器放入到ServletContext域中 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
</web-app>
  • 正常引入Struts2的Jar包

  • 创建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>


</struts>
  • 在web.xml中配置struts2的Filter
<?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">
    
    <!-- 配置Spring配置文件的名称和位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 启动IOC容器的ContextLoaderListener监听,将IOC容器放入到ServletContext域中 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置Struts2的Filter -->
    <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>
    
</web-app>
  • 创建pers.action.PersonAction
package pers.action;

import pers.service.PersonService;

public class PersonAction {
    private PersonService personService;

    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    public String execute() {
        System.out.println("personAction执行了。。。");
        personService.save();
        return "success";
    }
}
  • 创建pers.service.PersonService
package pers.service;

public class PersonService {
    public void save() {
        System.out.println("PersonService中save方法执行了。。。");
    }
}

  • 在applicationContext中配置bean(注意:将bean改成多例的)
<?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"
       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">


    <bean id="personService" class="pers.service.PersonService">
    </bean>

    <bean id="personAction" class="pers.action.PersonAction" scope="prototype">
        <property name="personService" ref="personService"></property>
    </bean>
</beans>
  • 配置Struts2配置文件
    注意(整合的关键):<action>中的class属性不再自己创建对象,而是用IOC容器提供该Action的对象(指向spring配置文件的Bean的id)
<?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="helloWorld" extends="struts-default" namespace="/">
        <action name="aaa" class="personAction">
            <result>success.jsp</result>
        </action>
    </package>

</struts>
  • 创建index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <a href="aaa.action">Test</a>
  </body>
</html>
  • 创建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<body>
成功了。。。
</body>
</html>
  • 运行结果
图片.png 图片.png 运行结果
  • 需要导入Struts2支持Spring的jar包 struts2-spring-plugin.jar (指明Struts2先在IOC容器中获取Action实例)

注意

struts2-spring-plugin.jar的版本不能超过struts2-core.jar,否则报错(最好两个版本一样)
配置文件放到src的直接目录下

相关文章

  • ssh框架整合

    ssh 整合思想 整合struts2 和spring 把struts2 的action对象创建交给spring进行...

  • Struts2与Spring整合

    前言 本博文主要讲解Spring怎么与Struts2框架整合... Struts2和Spring的整合关键点: a...

  • 15_Spring SSH整合准备

    SSH框架 SSH = Struts2 + Spring + Hibernate Struts2框架整合所需jar...

  • Spring学习 一 Struts2 与 Spring 整合

    Struts2框架和Spring整合的关键点就是: 把Struts2框架Action对象的创建,交给Spring ...

  • Spring整合Struts2

    Spring整合Struts2需要导入struts2-spring-plugin.jar文件。导入完成之后,str...

  • SSH整合(简要步骤)

    所谓ssh整合其实就是用Spring来管理Struts2以及Hibernate,通过依赖注入的方式。 1.整合原理...

  • SSH三大框架整合

    一.SSH三大框架整合知识点 1.全部知识点: Spring、Struts2 以及 Hibernate的整合思想。...

  • 2018-05-14

    星期一 今天做了 看spring框架视频学习标签的注入上课学习Struts2标签 内容 spring整合web项目...

  • SSH入门---框架搭建(eclipse环境下)

    前情提要:本文是把Spring、Struts2、Hibernate三大框架整合到一起,搭建整合框架的教程,如需查看...

  • Hibernate【与Spring整合】

    前言 前面已经学习了如何使用Spring与Struts2进行整合,本博文主要讲解如何使用Spring对Hibern...

网友评论

      本文标题:Spring整合Struts2

      本文链接:https://www.haomeiwen.com/subject/caczqhtx.html