【问题标题】:Force Spring Boot to use servlet mapping in web.xml强制 Spring Boot 在 web.xml 中使用 servlet 映射
【发布时间】:2017-07-22 11:44:21
【问题描述】:

我目前正在尝试将现有的动态 Web 项目转移到 Spring Boot 项目,它使用 web.xml 进行 servlet 映射。我知道spring会忽略web.xml文件,spring使用现有web.xml的正确方法应该是什么?是的,我仍然需要在这个项目中坚持使用 web.xml。

我对此有点陌生,请指导我!谢谢!

【问题讨论】:

    标签: spring-boot jar tomcat7 web.xml servlet-3.0


    【解决方案1】:

    这是一个非常古老的问题,但我目前处于同样的情况,我将分享我的经验。

    我扩展了 SpringBootServletInitializer 类来创建我的@SpringBootApplication,默认情况下它会包含你的 web.xml 配置。我仍然能够声明或编辑现有的 servlet 映射,这将被考虑在内。

    【讨论】:

      【解决方案2】:

      我认为您需要坚持使用 web.xml,因为您的容器使用的 Servlet 版本比 3.0 旧。

      Spring Boot 基于 Servlet 3.0 构建。您必须更新您的主类以扩展 SpringBootServletInitializer 并覆盖配置方法,该方法告诉 spring 使用其 Servlet 3.0 支持。像Tomcat这样的嵌入式容器需要Servlet 3.0,所以如果你想在嵌入式容器的开发过程(包括JUnit测试)中启动你的项目,我认为,据我所知,唯一的方法是将你的web.xml重写为Servlet 3.0爪哇配置。但是如果你真的想在旧的容器中部署你的应用程序,你仍然可以使用spring-boot-legacy 模块。它允许您将 web.xml 用于旧容器;您唯一需要做的就是添加 org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener 在您的 web.xml 中。

      有关在旧容器中部署战争的更多信息,请查看 Spring Boot 的official documentation

      Spring Boot 使用 Servlet 3.0 API 来初始化 ServletContext (注册 Servlet 等),因此您不能使用相同的应用程序 Servlet 2.5 容器中的框。但是,可以运行 带有一些特殊工具的旧容器上的 Spring Boot 应用程序。 如果您包含 org.springframework.boot:spring-boot-legacy 作为 依赖项(独立于 Spring Boot 的核心和 目前在 1.0.2.RELEASE 可用),您需要做的就是 创建一个 web.xml 并声明一个上下文监听器来创建 应用程序上下文以及您的过滤器和 servlet。

      【讨论】:

      • 感谢您的意见!我目前使用 servlet 3.0 和 Tomcat 7.0.75。我坚持使用 web.xml 的原因是因为其中有很多 servlet 映射,我只想先将其转换为 spring boot 项目。我确实有计划用正确的 web.xml 替换当前的 web.xml,比如 (servletcontainerinitializer)?
      猜你喜欢
      • 2015-03-01
      • 2013-01-20
      • 2012-01-02
      • 2013-09-24
      • 2012-04-17
      • 2012-07-23
      • 2016-07-05
      • 2016-06-30
      相关资源
      最近更新 更多