< mvc:default-servlet-handler/>的需求和用途是什么? [英] What is the need and use of <mvc:default-servlet-handler />
问题描述
Spring MVC中<mvc:default-servlet-handler />
的需求是什么.我们什么时候应该使用它.到底什么时候需要.我们为什么要使用它.我遍历了stackoverflow中的几个链接,但无法获得清晰的画面或理解.有人可以解释吗?
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 ?
推荐答案
Spring MVC
中<mvc:default-servlet-handler />
的需求是什么?
What is the need of <mvc:default-servlet-handler />
in Spring MVC
?
使用此handler
弹簧调度程序会将所有请求转发到默认的Servlet
.要启用该功能,您可以使用注释或基于xml的配置,如下所示:
Using this handler
spring dispatcher will forward all requests to the default Servlet
. To enable the feature either you can use annotations or xml based configuration as below:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
或使用XML:
<mvc:default-servlet-handler/>
它将做什么?
DefaultServletHttpRequestHandler
将在启动时使用大多数主要Servlet容器(包括Tomcat,Jetty,GlassFish,JBoss,Resin)的已知名称列表,尝试在启动时为container
的auto-detect
auto-detect
. ,WebLogic和WebSphere).如果默认Servlet是使用其他名称自定义配置的,或者在默认Servlet名称未知的情况下使用其他Servlet容器,则必须明确提供默认Servlet的名称,如以下示例所示:
The DefaultServletHttpRequestHandler
will attempt to auto-detect
the default Servlet
for the container
at 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:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable("myCustomDefaultServlet");
}
}
或使用XML:
<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/>
我们什么时候应该使用它?到底什么时候需要?我们为什么要使用它?
当您希望Spring调度程序使用默认servlet在web root
下提供static resources
时.
When you want spring dispatcher to serve static resources
under the web root
using default servlet.
如果使用的是DefaultServletHttpRequestHandler
,则可以替换:
If we are using DefaultServletHttpRequestHandler
, then we can replace :
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
与:
<mvc:default-servlet-handler />
More you can explore here.
这篇关于< mvc:default-servlet-handler/>的需求和用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!