default-servlet-handler 无法访问html,【SpringMVC问题】springmvc添加<mvc:default-servlet-handler/>后,静态资源可以访问,Co...

问题

springmvc添加或者后,静态资源可以访问,Controller不能访问,删掉后才能访问controller

具体配置文件

如果想要解决访问静态资源问题,通常会使用默认handler:

该标签的xsd文档说明如下:

/*配置一个handler通过转发到servlet容器的默认servlet来处理静态资源*/

Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet.

/*使用该handler允许DispatcherServlet 的url-patter 为'/',同时仍然使用servlet让其去处理静态资源*/

Use of this handler allows using a "/" mapping with the DispatcherServlet while still utilizing the Servlet container to serve static resources.

/*该handler将会转发所有的请求到默认serlvet*/

This handler will forward all requests to the default Servlet.

/*所以,在所有的URL HandlerMappings中,该handler对应的mapping应该留置最后!*/

Therefore it is important that it remains last in the order of all other URL HandlerMappings.

/*你可以使用两种方式去保证你的handlermapping 的order属性值小于DefaultServletHttpRequestHandler 对应的handlermapping的order属性值:使用标签或者手动配置HandlerMapping实例并设置其order属性值*/

That will be the case if you use the "annotation-driven" element

or alternatively if you are setting up your customized HandlerMapping instance

be sure to set its "order" property to a value lower

than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.

6799402de3a63d86b9a464db74706e5a.png

default-servlet-handler将在SpringMVC上下文中定义一个DefaultServletHttpRequestHandler。

DefaultServletHttpRequestHandler的javadoc如下:

/**

* An {@link HttpRequestHandler} for serving static files using the Servlet container's "default" Servlet.

*

*

This handler is intended to be used with a "/*" mapping when the

* {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}

* is mapped to "/", thus overriding the Servlet container's default handling of static resources.

* The mapping to this handler should generally be ordered as the last in the chain so that it will

* only execute when no other more specific mappings (i.e., to controllers) can be matched.

*

*

Requests are handled by forwarding through the {@link RequestDispatcher} obtained via the

* name specified through the {@link #setDefaultServletName "defaultServletName" property}.

* In most cases, the {@code defaultServletName} does not need to be set explicitly, as the

* handler checks at initialization time for the presence of the default Servlet of well-known

* containers such as Tomcat, Jetty, Resin, WebLogic and WebSphere. However, when running in a

* container where the default Servlet's name is not known, or where it has been customized

* via server configuration, the {@code defaultServletName} will need to be set explicitly.

*

* @author Jeremy Grelle

* @author Juergen Hoeller

* @since 3.0.4

*/

翻译如下

/*使用servlet容器的默认servlet处理静态资源*/

An {@link HttpRequestHandler} for serving static files using the Servlet container's "default" Servlet.

/*当DispatcherServlet url-pattern为"/"时,该handler将会使用"/*"去匹配请求路径;因此,重置了servlet容器对静态资源的默认处理*/

This handler is intended to be used with a "/*" mapping

when the {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} is mapped to "/",

thus overriding the Servlet container's default handling of static resources.

/*匹配该handler的mapping 应该是最后到达,当没有其他mapping可以处理请求时才会执行该handler匹配的mapping。*/

The mapping to this handler should generally be ordered as the last in the chain

so that it will only execute when no other more specific mappings (i.e., to controllers) can be matched.

标签:default,servlet,SpringMVC,mapping,访问,handler,DispatcherServlet,Servlet

来源: https://blog.csdn.net/qq_26838315/article/details/112572747

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值