Spring Boot中的DispatcherServlet和web.xml [英] DispatcherServlet and web.xml in Spring Boot

查看:136
本文介绍了Spring Boot中的DispatcherServlet和web.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将项目从Java EE迁移到Spring Boot项目。
然而,我一直对调度程序servlet和web.xml的部分感到困惑和困惑,似乎web.xml不再被项目读取了。当前项目在tomcat 7上运行。

I'm currently trying to move my project from Java EE to Spring Boot project. However, i've been stucked and confused on the part with dispatcher servlet and web.xml and it seems like web.xml is no longer being read by the project anymore. The current project is running on tomcat 7.

在我的 web.xml 文件中,我有很多 servlet servlet-mapping 过滤器过滤映射我真的不明白如何在调度程序中进行映射。

In my web.xml file, I have lots of servlet, servlet-mapping, filter and filter mapping and I don't really understand how to do the mapping in the dispatcher.

我附上了我的 web.xml 以下版本为2.5。

I've attached a sample of my web.xml below and the version is 2.5.

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>displayName</display-name>
  <description>description</description>
  <resource-ref>
    ...
  </resource-ref>
  <filter>
    <filter-name>Some Filter Name</filter-name>
    <filter-class>Some Filter Class</filter-class>
    <init-param>
      <param-name>Some Param Name</param-name>
      <param-value>Some Value</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Some Filter Name</filter-name>
    <url-pattern>Some url-pattern</url-pattern>
  </filter-mapping>
  <context-param>
    <param-name>Some Param Name</param-name>
    <param-value>Some Param Value</param-value>
  </context-param>
  <servlet>
    <servlet-name>Some Servlet Name</servlet-name>
    <servlet-class>Some Servlet Class</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Some Servlet Name</servlet-name>
    <url-pattern>Some Url Pattern</url-pattern>
  </servlet-mapping>
</web-app>

Qns:


  1. 我应该转换 web.xml中的所有内容吗? / code>依靠春季调度员,如果是,我怎么能实现呢?

  2. 正在离开 web.xml 去春季启动项目的方式?

  1. Should I convert all the stuff in my web.xml to rely on the spring dispatcher, if yes how can I achieve that?
  2. Is moving away from the web.xml the way to go for spring boot project?

任何人都可以在这里指导我吗?谢谢!!

Can anyone please guide me along here? Thanks!!

推荐答案


  1. 是的,Spring启动不再在xml配置上进行中继,它配置了一个等价物自动调度到servlet。您可以按照以下链接查看如何注册过滤器:如何在Spring Boot中添加过滤器类?

  2. 如果你使用maven而不是gradle,你的spring boot项目中唯一的XML应该是 pom.xml 。使用spring boot的方法是将所有xml配置,web.xml等移动到spring boot的自动配置+你的java配置。

  1. Yes, spring boot no longer relay on xml configuration and it configures an equivalent to the dispatcher servlet automatically. You can follow the following link to see how to register your filters: How to add a filter class in Spring Boot?
  2. If you use maven and not gradle, the only XML in your spring boot project should be pom.xml. The way to go with spring boot is moving all your xml configuration, web.xml etc to spring boot's auto configuration + your java configuration.

当您在java配置中执行所有操作并遵循其主体时,Spring启动非常有效。根据我的经验,当你开始合并XML配置和遗留弹簧时,它开始打破自动配置过程,并且更好地尽可能地尝试遵守新的Spring引导最佳实践。

Spring boot works very good when you do everything in java configuration and follow its principals. From my experience with it, when you start merging XML configuration and the legacy spring it starts breaking the auto configuration process and its much better to try as much as you can to comply with the new spring boot best practices.

这篇关于Spring Boot中的DispatcherServlet和web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆