spring怎么整合struts

fiy 其他 16

回复

共3条回复 我来回复
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    要将Spring和Struts整合在一起,你可以按照以下步骤进行操作:

    1. 添加相关依赖:在项目的pom.xml文件中添加Spring和Struts的依赖项。
    <!-- Spring依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
    
    <!-- Struts依赖 -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5.20</version>
    </dependency>
    
    1. 配置Struts的web.xml:在web.xml文件中,配置Struts的过滤器和监听器。
    <!-- Struts过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- Struts监听器 -->
    <listener>
        <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
    </listener>
    
    1. 创建Spring配置文件:创建一个Spring的配置文件,例如applicationContext.xml,用来配置Spring的相关组件。
    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.example"/>
    
    <!-- 配置Spring的WebMVC -->
    <mvc:annotation-driven/>
    
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
    </bean>
    
    1. 整合Spring和Struts:创建一个名为struts.xml的文件,用于配置Struts的相关内容,并将Spring的配置文件引入。
    <struts>
        <!-- 配置Struts的常量 -->
        <constant name="struts.devMode" value="true"/>
    
        <!-- 配置Spring的Action代理 -->
        <bean type="org.springframework.web.context.ContextLoaderListener"/>
    
        <!-- 引入Spring配置 -->
        <bean id="spring" class="org.springframework.web.context.support.AnnotationConfigWebApplicationContext">
            <property name="configLocation" value="classpath:applicationContext.xml"/>
        </bean>
    
        <!-- 配置Action包扫描 -->
        <package name="com.example" extends="struts-default">
            <action name="exampleAction" class="com.example.ExampleAction">
                <result>/example.jsp</result>
            </action>
        </package>
    </struts>
    
    1. 创建Action类:在项目中创建一个名为ExampleAction的Action类。
    package com.example;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class ExampleAction extends ActionSupport {
        private String message;
    
        public String execute() {
            message = "Hello, Spring and Struts!";
            return SUCCESS;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
    1. 创建JSP视图:在Web项目的WebContent目录下,创建一个名为example.jsp的JSP文件。
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Spring and Struts Integration Example</title>
    </head>
    <body>
        <h1>${message}</h1>
    </body>
    </html>
    
    1. 测试整合结果:启动项目,并在浏览器中访问示例Action的URL路径,例如http://localhost:8080/项目名/exampleAction,将会在浏览器中显示"Hello, Spring and Struts!"。

    通过以上的步骤,你已经成功地将Spring和Struts整合在一起,并可以实现Web应用的开发和管理。这种整合可以让你充分利用Spring的依赖注入和AOP特性,同时结合Struts的MVC框架,提升开发效率和代码质量。

    1年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    要实现Spring和Struts的整合,可以按照以下步骤进行操作:

    1. 引入相关依赖:在项目的pom.xml文件中添加Spring和Struts的相关依赖。可以通过Maven或手动下载相应的jar包,并将其添加到项目的classpath中。

    2. 配置Spring上下文:创建Spring的配置文件(如applicationContext.xml)并进行相关配置。配置文件中需要包含扫描注解的配置项,以及Struts Action类所在的包路径等信息。

    3. 整合Struts的配置文件:将Struts的配置文件(struts.xml)进行修改,以支持Spring的注入和管理。在配置文件中,需要添加对Spring的相关配置项,如使用Spring的Action代理工厂来管理Action的创建和依赖注入。

    4. 配置Spring的Action代理工厂:在Spring的配置文件中,要添加Action代理工厂的配置。可以使用Struts提供的DelegatingActionProxyFactory来实现Action对象的代理和Spring的依赖注入。

    5. 利用Spring来管理Action的依赖:在Struts的Action类中通过构造函数或Setter方法注入所需的依赖。依赖可以是其他的Service、DAO等Spring管理的Bean。

    需要注意的是,整合Spring和Struts的过程中,需要确保版本的兼容性,并且需要正确配置和注入需要的依赖项。另外,建议使用注解的方式来配置Spring,以简化配置的复杂性。整合完成后,可以使用Spring的IoC和AOP功能来实现依赖注入和事务管理等功能,结合Struts的MVC框架,实现更加灵活和易于维护的Web应用程序。

    1年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    Spring和Struts是两个非常流行的Java开发框架,它们分别以不同的方式处理Web应用程序的不同方面。Spring是一个轻量级的IoC(控制反转)和AOP(面向切面编程)容器,而Struts是一个MVC(模型-视图-控制器)框架。虽然它们可以单独使用,但在某些情况下,可能需要将它们整合在一起,以充分发挥它们各自的优势。

    整合Spring和Struts有助于简化应用程序的开发和维护。下面是整合Spring和Struts的一般步骤。

    1. 添加依赖关系和JAR包
      为了将Spring和Struts整合在一起,需要在项目的构建文件中添加相应的依赖关系。

    对于Maven项目,在pom.xml文件中添加以下依赖:

    <dependencies>
       <!-- Spring -->
       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.2.0.RELEASE</version>
       </dependency>
    
       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.0.RELEASE</version>
       </dependency>
    
       <!-- Struts -->
       <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-core</artifactId>
          <version>2.5.20</version>
       </dependency>
    </dependencies>
    

    对于非Maven项目,需要手动将相应的JAR包添加到项目的Classpath中。

    1. 配置Spring和Struts的配置文件
      在整合Spring和Struts时,需要在项目中创建两个配置文件:Spring的配置文件和Struts的配置文件。我们将分别命名为applicationContext.xmlstruts.xml

    applicationContext.xml中,配置Spring的IoC容器和AOP:

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.example"/>
    
    <!-- 配置Struts的Action访问Spring的Bean -->
    <bean id="strutsBeanLookup" class="com.example.StrutsBeanLookup">
       <property name="service" ref="userService"/>
    </bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
    </bean>
    

    struts.xml中,配置Struts的Action和结果映射:

    <struts>
       <package name="default" extends="struts-default">
          <interceptors>
             <interceptor name="spring" class="org.springframework.web.struts.DelegatingActionUtils"/>
             <interceptor-stack name="defaultStack">
                <interceptor-ref name="spring"/>
                <interceptor-ref name="defaultStack"/>
             </interceptor-stack>
          </interceptors>
    
          <default-interceptor-ref name="defaultStack"/>
    
          <action name="exampleAction" class="com.example.ExampleAction">
             <result>/example.jsp</result>
          </action>
       </package>
    </struts>
    
    1. 创建Action类
      创建一个继承自org.springframework.web.struts.DelegatingActionProxy的Action类。这个Action类将用于在Struts的Action中访问Spring的Bean。
    public class StrutsBeanLookup extends DelegatingActionProxy {
       private UserService service; // 注入依赖
    
       public void setService(UserService service) {
          this.service = service;
       }
    
       @Override
       protected ActionComponent getDelegateAction(Object action) {
          return new ActionComponentWrapper(action, service);
       }
    }
    

    在这个Action类中,我们注入了一个UserService的依赖,并将其传递给ActionComponentWrapper类。

    public class ActionComponentWrapper extends ActionComponent {
       private UserService service;
    
       public ActionComponentWrapper(ActionInvocation invocation, UserService service) {
          super(invocation);
          this.service = service;
       }
    
       @Override
       public Object invoke() throws Exception {
          // 在这里可以调用Spring的Bean
          service.doSomething();
    
          return super.invoke();
       }
    }
    

    这个ActionComponentWrapper类将被用作Struts的Action类,在其中可以访问Spring的Bean。

    1. 配置Web应用程序上下文
      web.xml文件中配置Spring的ContextLoaderListener
    <web-app>
       ...
       <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
    
       <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
             classpath:applicationContext.xml
          </param-value>
       </context-param>
       ...
    </web-app>
    
    1. 配置Struts过滤器
      web.xml文件中添加Struts的过滤器:
    <web-app>
       ...
       <filter>
          <filter-name>struts2</filter-name>
          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
       </filter>
    
       <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
       </filter-mapping>
       ...
    </web-app>
    

    通过以上步骤,我们成功地将Spring和Struts整合在一起,可以使用Spring的IoC容器来管理Struts的Action以及其他组件。同时,还可以利用Spring提供的AOP功能在Struts的Action中应用横切关注点,例如事务管理等。

    需要注意的是,通过将Spring和Struts整合在一起,可以充分发挥它们各自的优势,但同时也增加了应用程序的复杂性。因此,在整合之前,建议仔细考虑是否真正需要使用这种整合方式,以及是否值得为此投入额外的开发和维护成本。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部