SpringMVC错误之No mapping found for HTTP request with [url]
程序员文章站
2022-03-08 16:12:58
...
- spring的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.2.xsd">
<bean id="/productSave.action" class="Controller.ProductSaveController"></bean>
<bean id="/productForm.action" class="Controller.ProductFormController"></bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
其中配置了视图解析器viewResolver,ProductSaveController和ProductFormController两个实现了org.springframework.web.servlet.mvc.Controller接口的实现类根据不同的url返回不同的视图路径,ProductFormController的代码如下:
package Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ProductFormController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
//这里只返回了一个视图对象
return new ModelAndView("productFrom");
}
}
- web.xml的配置如下:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 错误原因:这里url-pattern拦截了所有请求,报错原因就在于当IOC容器注入一个bean时(比如id=”/productForm.action”的bean),该bean将返回一个view的路径,而 DispatcherServlet又将该view的路径当作另一个请求,再次对其进行拦截,而此时spring的配置文件中并没有对这个路径进行映射,因此报错。
- 解决方案:
- 更改拦截url,只拦截以.action结尾的请求:
<url-pattern>*.action</url-pattern>
- 在spring的配置文件中配置访问静态资源的servlet:
<mvc:default-servlet-handler/>
- 更改拦截url,只拦截以.action结尾的请求:
上一篇: 【问题场景】静态文件请求错误No mapping found for HTTP request with URI 解决
下一篇: java date the type is ambiguous_java 调用方法引起歧义:The method XXX is ambiguous for the type XX...
推荐阅读
-
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 [/springmvc-01/helloworld] in DispatcherServlet with
-
No mapping found for HTTP request with URI [/app17a/] in DispatcherServlet with name 'springmvc'解决方法
-
No mapping found for HTTP request with URI [/Springmvc/test] in DispatcherServlet with dispatche
-
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
-
【问题场景】静态文件请求错误No mapping found for HTTP request with URI 解决
-
SpringMVC错误之No mapping found for HTTP request with [url]
-
SpringMVC错误:No mapping found for HTTP request with URI [xxxx] in DispatcherServlet