通过 6 个步骤从 Struts 迁移到 Spring MVC

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

在本教程中,我们将看到用 struts 制作的 Web 应用程序迁移到 spring mvc。 Struts到Spring的迁移我们会一步步来做。在这里,我将逐步与您分享使用注解将应用程序从 Struts 迁移到 Spring 的方法。

第 1 步:将 struts 库替换为 spring 库。

首先,在从 struts 迁移到 spring 时,我们必须将 struts 相关的库替换为 lib 文件夹中的 spring 库。


为了您的澄清,我已经提到了 struts 和 spring 的基本库。

Struts 基础库:

  1. struts.jar

  2. struts-legacy.jar

  3. ETC..

你用过吗:Java 中的 Javadoc 注释

弹簧基础库:

  1. 标准.jar

  2. org.springframework.asm-4.0.1.RELEASE-A.jar

  3. org.springframework.beans-4.0.1.RELEASE-A.jar

  4. org.springframework.context-4.0.1.RELEASE-A.jar

  5. org.springframework.core-4.0.1.RELEASE-A.jar

  6. org.springframework.expression-4.0.1.RELEASE-A.jar

  7. org.springframework.web.servlet-4.0.1.RELEASE-A.jar

  8. org.springframework.web-4.0.1.RELEASE-A.jar

  9. ETC..

第 2 步:将 struts 的 web.xml 文件更改为 spring 迁移

在这一步中,我们必须删除 web.xml 的 Action 过滤器调度程序并添加 Spring dipatcher servlet 作为前端控制器

致力于新技术:使用 Java 创建和管理云应用程序

Strut 应用程序中,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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>Struts2MyFirstApp</display-name>

<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>

<welcome-file-list>

<welcome-file>Login.jsp</welcome-file>

</welcome-file-list>

</web-app>


Spring 应用程序中,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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>Struts2MyFirstApp</display-name>

<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>

<welcome-file-list>

<welcome-file>Login.jsp</welcome-file>

</welcome-file-list>

</web-app>


第 3 步:替换 struts 的配置文件以进行 spring 迁移

现在将所有struts配置文件替换成spring配置文件如下

Struts 应用程序中的 struts 配置文件 -


 <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>Struts2MyFirstApp</display-name>

<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>

<welcome-file-list>

<welcome-file>Login.jsp</welcome-file>

</welcome-file-list>

</web-app>


Spring中应用spring配置文件 如下


 <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>Struts2MyFirstApp</display-name>

<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>

<welcome-file-list>

<welcome-file>Login.jsp</welcome-file>

</welcome-file-list>

</web-app>


在这里,使用了 <context:component-scan> 标签,这样 spring 将从给定的包中加载所有组件,即“com.geekonjavaonjava.spring.login.controller”。

我们可以使用不同的视图解析器,这里我使用了 InternalResourceViewResolver。其中prefix和suffix用于通过给action类中ModelAndView返回的对象添加前缀和后缀值来解析视图。

其他三个步骤见参考