spring <mvc:default-servlet-handler /> 的需要和用途是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31346267/
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-09-08 00:40:51  来源:igfitidea点击:

What is the need and use of <mvc:default-servlet-handler />

springspring-mvc

提问by Avinash Reddy

What is the need of <mvc:default-servlet-handler />in Spring MVC . When should we use it. When exactly is it needed. Why should we use it. I gone through few links in stackoverflow, but could not get clear picture or understanding. Can someone explain ?

<mvc:default-servlet-handler />Spring MVC需要什么。我们什么时候应该使用它。什么时候需要它。我们为什么要使用它。我在stackoverflow中浏览了几个链接,但无法获得清晰的图片或理解。有人可以解释一下吗?

回答by Arpit Aggarwal

What is the need of <mvc:default-servlet-handler />in Spring MVC?

什么是必要<mvc:default-servlet-handler />Spring MVC

Using this handlerspring dispatcher will forward all requests to the default Servlet. To enable the feature either you can use annotations or xml based configuration as below:

使用这个handlerspring 调度器会将所有请求转发到默认的Servlet. 要启用该功能,您可以使用注释或基于 xml 的配置,如下所示:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

Or in XML:

或者在 XML 中:

<mvc:default-servlet-handler/>

What it will do?

它会做什么?

The DefaultServletHttpRequestHandlerwill attempt to auto-detectthe default Servletfor the containerat startup time, using a list of known names for most of the major Servlet containers (including Tomcat, Jetty, GlassFish, JBoss, Resin, WebLogic, and WebSphere). If the default Servlet has been custom configured with a different name, or if a different Servlet container is being used where the default Servlet name is unknown, then the default Servlet's name must be explicitly provided as in the following example:

DefaultServletHttpRequestHandler将尝试auto-detectdefault Servletcontainer在启动时,使用大多数主要的Servlet容器(包括软件Tomcat,Jetty的GlassFish,JBoss和树脂中,WebLogic和WebSphere)已知名称的列表。如果默认 Servlet 已自定义配置为不同的名称,或者如果在默认 Servlet 名称未知的情况下使用不同的 Servlet 容器,则必须明确提供默认 Servlet 的名称,如下例所示:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable("myCustomDefaultServlet");
    }
}

Or in XML:

或者在 XML 中:

<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/>

When should we use it? When exactly is it needed? Why should we use it?

我们应该什么时候使用它?什么时候需要它?我们为什么要使用它?

When you want spring dispatcher to serve static resourcesunder the web rootusing default servlet.

当您希望 spring 调度程序static resourcesweb root使用默认 servlet 下提供服务时。

If we are using DefaultServletHttpRequestHandler, then we can replace :

如果我们正在使用DefaultServletHttpRequestHandler,那么我们可以替换:

    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/images/**" location="/images/" />

with :

和 :

<mvc:default-servlet-handler />

More you can explore here.

你可以在这里探索更多。

回答by HangGao

Because we usually config the DispatcherServlet with the mapping "/*",but all requests such as .js/.css will also be mapped to DispatcherServlet,so we need a HttpRequestHandler to dispatch these static resources requests to DefaultServlet.

因为我们通常将 DispatcherServlet 配置为映射“/*”,但是所有的请求如.js/.css 也会映射到 DispatcherServlet,所以我们需要一个 HttpRequestHandler 将这些静态资源请求分派到 DefaultServlet。

回答by wakedeer

<mvc:default-servlet-handler />

This tag usually use for getting resourses like *.js, *.css, *.jpg and etc.

此标签通常用于获取资源,如 *.js、*.css、*.jpg 等。

Tag mvc:default-servlet-handler use DefaultServletHttpRequestHandler which has low property(Integer.MAX_VALUE) than handlers in mvc:annotation-driven:

标记 mvc:default-servlet-handler 使用 DefaultServletHttpRequestHandler ,它的属性(Integer.MAX_VALUE) 比mvc:annotation-driven 中的处理程序

  • DefaultAnnotationHandlerMapping
  • AnnotationMethodHandlerAdapter
  • AnnotationMethodHandlerExceptionResolve
  • see here
  • DefaultAnnotationHandlerMapping
  • AnnotationMethodHandlerAdapter
  • AnnotationMethodHandlerExceptionResolve
  • 看这里

For example

例如

Your request like ( http://localhost:8080/jquery.js) first will be search Controller mapping @RequsetMapping("jquery.js"), and when request don't find any Controller mapping try to get resourse in your folder webor webapp.

您的请求(http://localhost:8080/jquery.js)首先将搜索控制器映射@RequsetMapping("jquery.js"),当请求未找到任何控制器映射时,尝试在您的文件夹web 中获取资源或webapp

If you have webapp/jquery.js, you get this file.

如果你有webapp/jquery.js,你会得到这个文件。