Spring No mapping found for HTTP request with URI解决办法
程序员文章站
2022-07-15 13:11:46
...
Spring No mapping found for HTTP request with URI问题解决
一. 问题描述
最近项目中使用Spring注解配置时,出现了以下异常问题.
部署项目后程序加载或用浏览器访问时出现类似警告:
2011-01-19 10:52:51,646 WARN [org.springframework.web.servlet.PageNotFound] -<No mapping found for HTTP request with URI [/sandDemo001/images/1.jpg] in DispatcherServlet with name 'spring'>
主要看尖括号里面的部分:
<No mapping found for HTTP request with URI [/sandDemo001/images/1.jpg] in DispatcherServlet with name 'spring'>
二. 问题原因
原因是web.xml文件中,Spring的DispatcherServlet对url的配置不合理,原配置如下:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
其中 <servlet-mapping>的<url-pattern>/</url-pattern>
把所有的请求都交给Spring去处理了,而所有可用的请求url都是在Constroller里使用类似@RequestMapping(value = "/login/{user}", method = RequestMethod.GET)
这样的注解配置的,这样当我们对html/js/css/jpg/gif
等静态资源访问时就会请求不到.
三. 解决方法一,在web.xml里添加如下的配置:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
四. 解决方法二,在Spring的配置文件中添加如下代码:
<mvc:default-servlet-handler/>
注意:
需要是spring3.0.5以上版本!!
五. 解决方法三
Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory
<mvc:resources mapping="/resources/**" location="/resources/"/>
这个配置是告诉Spring静态资源的处理方式.
上一篇: No mapping found for HTTP request with URI
下一篇: 待解决 No mapping found for HTTP request with URI [/cloudPlatformSystem/login.action] in DispatcherS
推荐阅读
-
No mapping found for HTTP request with URI [/springmvc-1/springmvc/testParamsAndHeaders] in Dispatch
-
vue+No mapping found for HTTP request with URI [*//**] in DispatcherServlet with name ‘springMvc‘
-
异常记录二:寻求解决异常 !No mapping found for HTTP request with URI
-
如何解决“ No mapping found for HTTP request with URI [XXX] in DispatcherServlet with name 'XXX' ”
-
警告: No mapping found for HTTP request with URI [/springmvc-01/helloworld] in DispatcherServlet with
-
No mapping found for HTTP request with URI [/xxx/xxx] in DispatcherServlet with name 'xxx'
-
No mapping found for HTTP request with URI [...] in DispatcherServlet with name ‘DispatcherServlet‘
-
No mapping found for HTTP request with URI [***.html] in DispatcherServlet with name "***"
-
No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb...
-
No mapping found for HTTP request with URI [/app17a/] in DispatcherServlet with name 'springmvc'解决方法