Spring MVC没有看到资源文件夹

时间:2017-04-27 13:37:16

标签: spring spring-mvc

即使添加<mvc:resources mapping="/resources/**" location="/resources"/>

,Spring也看不到资源文件夹

我正在键入localhost:8080/resources/qwerty.txt(文件已创建),而Dispatcher servlet正在通过ViewResolver解决它,如“/WEB-INF/views/resources/qwerty.jsp”

此外,我在github上找到了一个类似的项目,“webapp”中有“resorces”文件夹,一切正常。按照我上面描述的相同程序 - spring在屏幕上显示qwerty.txt。

这是Tomcat控制台:

请求#1:

TRACE   2017-04-27 15:32:23,455 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Bound request context to thread: org.apache.catalina.connector.RequestFacade@4dbf1eec
DEBUG   2017-04-27 15:32:23,456 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'di' processing GET request for [/resources/qwerty.txt]
TRACE   2017-04-27 15:32:23,460 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@444835f1] in DispatcherServlet with name 'di'
DEBUG   2017-04-27 15:32:23,463 [http-bio-8080-exec-1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Looking up handler method for path /resources/qwerty.txt
TRACE   2017-04-27 15:32:23,473 [http-bio-8080-exec-1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Found 1 matching mapping(s) for [/resources/qwerty.txt] : [{[],methods=[GET]}]
DEBUG   2017-04-27 15:32:23,473 [http-bio-8080-exec-1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Returning handler method [public void web.SimpleController.index()]
DEBUG   2017-04-27 15:32:23,474 [http-bio-8080-exec-1] org.springframework.beans.factory.support.DefaultListableBeanFactory  - Returning cached instance of singleton bean 'simpleController'
TRACE   2017-04-27 15:32:23,475 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@75ad2983]
TRACE   2017-04-27 15:32:23,475 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@1d7ba137]
TRACE   2017-04-27 15:32:23,476 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@2efb2c32]
DEBUG   2017-04-27 15:32:23,476 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Last-Modified value for [/resources/qwerty.txt] is: -1
TRACE   2017-04-27 15:32:23,500 [http-bio-8080-exec-1] org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod  - Invoking [SimpleController.index] method with arguments []
TRACE   2017-04-27 15:32:23,500 [http-bio-8080-exec-1] org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod  - Method [index] returned [null]
DEBUG   2017-04-27 15:32:23,513 [http-bio-8080-exec-1] org.springframework.beans.factory.support.DefaultListableBeanFactory  - Invoking afterPropertiesSet() on bean with name 'resources/qwerty'
TRACE   2017-04-27 15:32:23,513 [http-bio-8080-exec-1] org.springframework.web.servlet.view.InternalResourceViewResolver  - Cached view [resources/qwerty]
DEBUG   2017-04-27 15:32:23,513 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Rendering view [org.springframework.web.servlet.view.JstlView: name 'resources/qwerty'; URL [/WEB-INF/views/resources/qwerty.jsp]] in DispatcherServlet with name 'di'
TRACE   2017-04-27 15:32:23,513 [http-bio-8080-exec-1] org.springframework.web.servlet.view.JstlView  - Rendering view with name 'resources/qwerty' with model {} and static attributes {}
DEBUG   2017-04-27 15:32:23,521 [http-bio-8080-exec-1] org.springframework.web.servlet.view.JstlView  - Forwarding to resource [/WEB-INF/views/resources/qwerty.jsp] in InternalResourceView 'resources/qwerty'
TRACE   2017-04-27 15:32:23,527 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@4dbf1eec
DEBUG   2017-04-27 15:32:23,527 [http-bio-8080-exec-1] org.springframework.web.servlet.DispatcherServlet  - Successfully completed request
TRACE   2017-04-27 15:32:23,529 [http-bio-8080-exec-1] org.springframework.web.context.support.XmlWebApplicationContext  - Publishing event in WebApplicationContext for namespace 'di-servlet': ServletRequestHandledEvent: url=[/resources/qwerty.txt]; client=[127.0.0.1]; method=[GET]; servlet=[di]; session=[null]; user=[null]; time=[89ms]; status=[OK]

请求#2:

TRACE   2017-04-27 15:32:49,272 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Bound request context to thread: org.apache.catalina.connector.RequestFacade@4dbf1eec
DEBUG   2017-04-27 15:32:49,273 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'di' processing GET request for [/resources/qwerty.txt]
TRACE   2017-04-27 15:32:49,274 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@444835f1] in DispatcherServlet with name 'di'
DEBUG   2017-04-27 15:32:49,274 [http-bio-8080-exec-3] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Looking up handler method for path /resources/qwerty.txt
TRACE   2017-04-27 15:32:49,275 [http-bio-8080-exec-3] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Found 1 matching mapping(s) for [/resources/qwerty.txt] : [{[],methods=[GET]}]
DEBUG   2017-04-27 15:32:49,275 [http-bio-8080-exec-3] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Returning handler method [public void web.SimpleController.index()]
DEBUG   2017-04-27 15:32:49,275 [http-bio-8080-exec-3] org.springframework.beans.factory.support.DefaultListableBeanFactory  - Returning cached instance of singleton bean 'simpleController'
TRACE   2017-04-27 15:32:49,276 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@75ad2983]
TRACE   2017-04-27 15:32:49,276 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@1d7ba137]
TRACE   2017-04-27 15:32:49,276 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@2efb2c32]
DEBUG   2017-04-27 15:32:49,276 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Last-Modified value for [/resources/qwerty.txt] is: -1
TRACE   2017-04-27 15:32:49,277 [http-bio-8080-exec-3] org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod  - Invoking [SimpleController.index] method with arguments []
TRACE   2017-04-27 15:32:49,284 [http-bio-8080-exec-3] org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod  - Method [index] returned [null]
DEBUG   2017-04-27 15:32:49,287 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Rendering view [org.springframework.web.servlet.view.JstlView: name 'resources/qwerty'; URL [/WEB-INF/views/resources/qwerty.jsp]] in DispatcherServlet with name 'di'
TRACE   2017-04-27 15:32:49,287 [http-bio-8080-exec-3] org.springframework.web.servlet.view.JstlView  - Rendering view with name 'resources/qwerty' with model {} and static attributes {}
DEBUG   2017-04-27 15:32:49,288 [http-bio-8080-exec-3] org.springframework.web.servlet.view.JstlView  - Forwarding to resource [/WEB-INF/views/resources/qwerty.jsp] in InternalResourceView 'resources/qwerty'
TRACE   2017-04-27 15:32:49,289 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@4dbf1eec
DEBUG   2017-04-27 15:32:49,289 [http-bio-8080-exec-3] org.springframework.web.servlet.DispatcherServlet  - Successfully completed request
TRACE   2017-04-27 15:32:49,289 [http-bio-8080-exec-3] org.springframework.web.context.support.XmlWebApplicationContext  - Publishing event in WebApplicationContext for namespace 'di-servlet': ServletRequestHandledEvent: url=[/resources/qwerty.txt]; client=[127.0.0.1]; method=[GET]; servlet=[di]; session=[null]; user=[null]; time=[18ms]; status=[OK]

servlet上下文:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="controller, model, service, todo, web"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/views/"
      p:suffix=".jsp"
/>

<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:annotation-driven />

我做错了什么?

1 个答案:

答案 0 :(得分:0)

解决:

我刚刚将某些方法的映射从默认更改为实名,一切都消失了

魔术

有谁知道为什么它以这种奇怪的方式工作?