Java Spring Boot 中的 DispatcherServlet 和 web.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43225549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:47:02  来源:igfitidea点击:

DispatcherServlet and web.xml in Spring Boot

javaspringspring-boottomcat7web.xml

提问by NatureWonder

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.

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

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

在我的web.xml文件,我有很多的servletservlet-mappingfilterfilter mapping我真的不知道该怎么办的调度映射。

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

我在下面附上了一个样本,web.xml版本是 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. Should I convert all the stuff in my web.xmlto rely on the spring dispatcher, if yes how can I achieve that?
  2. Is moving away from the web.xmlthe way to go for spring boot project?
  1. 我是否应该将所有内容转换web.xml为依赖 spring 调度程序,如果是,我该如何实现?
  2. 正在远离web.xmlSpring Boot项目的道路吗?

Can anyone please guide me along here? Thanks!!

有人可以在这里指导我吗?谢谢!!

采纳答案by Tom

  1. Yes, spring boot no longer relies 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.
  1. 是的,spring boot 不再依赖于 xml 配置,它会自动配置一个相当于调度程序 servlet 的东西。您可以按照以下链接查看如何注册您的过滤器:如何在 Spring Boot 中添加过滤器类?
  2. 如果您使用 maven 而不是 gradle,那么您的 Spring Boot 项目中唯一的 XML 应该是pom.xml. 使用 spring boot 的方法是将所有 xml 配置、web.xml 等移动到 spring boot 的自动配置 + 您的 java 配置。

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.

当您在 Java 配置中完成所有操作并遵循其原则时,Spring Boot 会非常有效。根据我的经验,当您开始合并 XML 配置和遗留 spring 时,它开始破坏自动配置过程,最好尽可能多地尝试遵守新的 spring 引导最佳实践。

回答by Md Jalal

  1. You can keep your web.xml, but it needs to add

    <listener>
         <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
    </listener> 
    

    in web.xml. And, required dependency of pom.xml.

  2. All listener classes, filters converts in Java class. This class would be @Configuration.

  3. If you have an interceptor, that can be moved to configuration class.

  1. 您可以保留您的web.xml,但需要添加

    <listener>
         <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
    </listener> 
    

    web.xml。并且,需要依赖pom.xml.

  2. 所有侦听器类、过滤器都在 Java 类中转换。这门课将是@Configuration.

  3. 如果您有拦截器,则可以将其移至配置类。

回答by Ananthapadmanabhan

Spring-boot prefer annotations over xmlbased configurations, so in your case instead of using web.xmlto configure the servlet, servlet-mapping, filterand filter mapping, you can use annotation based automatic bean creations to register beans.For that you need to :

Spring-boot 比基于xml的配置更喜欢注解,因此在您的情况下,您可以使用基于注解的自动 bean 创建来注册 bean,而不是web.xml用于配置servlet, servlet-mapping, filterand filter mapping。为此,您需要:

  • Convert the xml based mappings to annotation based mappings
  • Create beans using @Beanannotations so that spring-boot will automatically take them up during component scan.
  • 将基于 xml 的映射转换为基于注释的映射
  • 使用@Bean注解创建 bean,以便 spring-boot 在组件扫描期间自动获取它们。

For reference: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

供参考:https: //docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

  • For registering filters and adding filter beansyou can create a class annotate it with the @Configurationor @Componentannotation and create bean of FilterRegistrationBeanto register the filter.You can also create the beans of filter itself there by using @Bean annotation.
  • 对于注册过滤器和添加过滤器 bean,您可以创建一个类,使用@Configurationor@Component注释对其进行注释,并创建 bean ofFilterRegistrationBean以注册过滤器。您还可以使用 @Bean 注释在那里创建过滤器本身的 bean。

For example, the equivalent of the following xml based filter

例如,相当于以下基于 xml 的过滤器

<filter>
  <filter-name>SomeUrlFilter</filter-name>
   <filter-class>com.company.SomeUrlFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SomeUrlFilter</filter-name>
    <url-pattern>/url/*</url-pattern>
    <init-param>
       <param-name>paramName</param-name>
       <param-value>paramValue</param-value>
    </init-param>
</filter-mapping>

The equivalent annotation based will be:

基于等效注释将是:

@Bean
public FilterRegistrationBean someUrlFilterRegistration() {

    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(someUrlFilter());
    registration.addUrlPatterns("/url/*");
    registration.addInitParameter("paramName", "paramValue");
    registration.setName("Filter");
    registration.setOrder(1);
    return registration;
} 

@Bean(name = "someUrlFilter")
public Filter someUrlFilter() {
    return new SomeUrlFilter();
}
  • Springboot still allows us to use the xml based configurations for example if you want to use the web.xml.For example :
  • Springboot 仍然允许我们使用基于 xml 的配置,例如,如果您想使用web.xml.For example:

Web.xml

网页.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/dispatcher.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

and in another file dispatcher.xmlyou can create beans as :

在另一个文件dispatcher.xml 中,您可以将 bean 创建为:

<beans ...>

    <context:component-scan base-package="com.demo"/>

    <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

Note that Spring web.xmlwill usually live in src/main/webapp/WEB-INF.

请注意,Springweb.xml通常位于src/main/webapp/WEB-INF.

You can refer : https://www.baeldung.com/register-servlet

您可以参考:https: //www.baeldung.com/register-servlet

回答by Vikky

Spent quite some time so sharing three things I remember to make it working from command line.

花了相当多的时间,所以我记得分享三件事,让它从命令行工作。

  1. Most important: JSPs should be kept only in folder /src/main/resources/META-INF/resources. more info here
  2. Make sure that maven while building jar is considering your folders /src/main/resources, otherwise add these lines in your pom.xml
  1. 最重要的是:JSP 应该只保存在文件夹 /src/main/resources/META-INF/resources 中。更多信息在这里
  2. 确保在构建 jar 时 maven 正在考虑您的文件夹 /src/main/resources,否则在您的 pom.xml 中添加这些行

  <resources>
     <resource>
     <directory>src/main/resources</directory>   
      </resource>
    </resources>

  1. In your mvc config file

    @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/Representation/"); resolver.setSuffix(".jsp"); return resolver; }

  1. 在你的 mvc 配置文件中

    @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/Representation/"); resolver.setSuffix(".jsp"); 返回解析器;}