美文网首页
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的直接目录下

    相关文章

      网友评论

          本文标题:Spring整合Struts2

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