gpt4 book ai didi

java - Spring MVC配置启用

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:44 24 4
gpt4 key购买 nike

我正在从头开始建立一个项目,目前我正在使用 java config 配置 Spring MVC 4.1.5。整个应用程序正在 tomcat gradle 插件上运行。

有人能解释一下为什么我需要对类 DefaultServletHandlerConfigurer 进行以下调用,以便将请求映射到我的 Controller 吗?

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

如果不启用它,我的所有请求都会被拒绝,服务器说没有针对特定请求的映射。

我读 Spring doc找出答案,但描述并没有告诉我太多信息。

Enable forwarding to the "default" Servlet. When this method is used the DefaultServletHttpRequestHandler will try to auto-detect the "default" Servlet name. Alternatively, you can specify the name of the default Servlet via enable(String).

最佳答案

它在 Spring MVC 部分文档中解释得很好 here .

This allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container’s default Servlet), while still allowing static resource requests to be handled by the container’s default Servlet. It configures a DefaultServletHttpRequestHandler with a URL mapping of "/**" and the lowest priority relative to other URL mappings.

This handler will forward all requests to the default Servlet. Therefore it is important that it remains last in the order of all other URL HandlerMappings. That will be the case if you use or alternatively if you are setting up your own customized HandlerMapping instance be sure to set its order property to a value lower than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.

那段代码相当于xml行<mvc:default-servlet-handler/>在通常定义为 servletname-servlet.xml 的 Spring Web MVC 特定组件文件中

您必须使用该调用才能配置转发到 Spring 默认 Servlet,如果您不这样做,您的服务器将尝试使用他自己的 servlet 处理,正如您所解释的,如果您没有任何特定的定义它不会为您的请求找到任何映射。

关于java - Spring MVC配置启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com