问题
:在WebContent目录下放置了一张image.png图片:
使用路径http://localhost:8080/SpringMVCProject/image.png直接访问这个静态资源,但是出现了错误:
报错的信息如下
:
HTTP Status 500 – Internal Server Error
Type 异常报告
消息 Request processing failed; nested exception is java.lang.IllegalStateException: A RequestDispatcher could not be located for the default servlet 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler'
描述 服务器遇到一个意外的情况,阻止它完成请求。
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: A RequestDispatcher could not be located for the default servlet 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler'
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Root Cause
java.lang.IllegalStateException: A RequestDispatcher could not be located for the default servlet 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler'
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler.handleRequest(DefaultServletHttpRequestHandler.java:124)
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:53)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
)
报错的截图
:
下面列出项目的一些基本信息:
springmvc.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 扫描有注解的包 -->
<context:component-scan base-package="org.lanqiao.handler"></context:component-scan>
<!-- 配置视图解析器(InternalResourceViewResolver) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 加载国际化资源文件
1.将ResourceBundleMessageSource在程序加载的时候,加入springmvc:springmvc在启动的时候,会自动查找一个id="messageSource"的bean,
如果有的话,自动进行加载;
2.如果配置了ResourceBundleMessageSource,则该类会在程序响应的时候介入
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
<!-- view-name会被视图解析器加上前缀和后缀 -->
<mvc:view-controller path="springmvcHandler/testMvcViewController" view-name="success"/>
<!-- 此配置是SpringMVC基础的配置,很多功能都需要通过这个注解进行协调 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 该注解使得SpringMVC接受一个请求时,若该请求没有对应的@RquestMapping,则将该请求交给服务器对应的Serlet -->
<!-- 这样就可以访问静态资源,同时需要加上mvc:annotation-driven协调,使得既可以访问静态资源,也可以访问动态资源 -->
<mvc:default-servlet-handler default-servlet-name="org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler"></mvc:default-servlet-handler>
<!-- 配置数据格式化所需要的Bean -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean>
</beans>
web.xml配置信息:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVCProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 通过以下的配置拦截所有的请求,交给springmvc处理 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- springmvc的入口 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- servlet需要一个参数指定springmvc.xml的位置 -->
<!-- 可以省略,默认的文件位置是/WEB-INF/springDispatcherServlet-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--
注意springmvc用/而不是/*匹配所有的请求
/user:拦截以/user开头的请求
/user/abc.do:只拦截该请求
.action:只拦截.action结尾的请求
可以通过拦截不同的请求,决定使用servlet或者springmvc处理
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 增加HiddenHttpMethodFilter:目的是给普通的浏览器添加PUT|DELETE方法的请求-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我是通过配置:
希望达到能够访问image.png的目的:但是没有成功