SpringMVC错误之No mapping found for HTTP request with [url]
2017-09-09 10:05
369 查看
spring的配置如下:
其中配置了视图解析器viewResolver,ProductSaveController和ProductFormController两个实现了org.springframework.web.servlet.mvc.Controller接口的实现类根据不同的url返回不同的视图路径,ProductFormController的代码如下:
web.xml的配置如下:
错误原因:这里url-pattern拦截了所有请求,报错原因就在于当IOC容器注入一个bean时(比如id=”/productForm.action”的bean),该bean将返回一个view的路径,而 DispatcherServlet又将该view的路径当作另一个请求,再次对其进行拦截,而此时spring的配置文件中并没有对这个路径进行映射,因此报错。
解决方案:
更改拦截url,只拦截以.action结尾的请求:
在spring的配置文件中配置访问静态资源的servlet:
<?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/>
相关文章推荐
- idea部署SpringMVC项目出现No mapping found for HTTP request with URI [xxxxxxx] in DispatcherServlet ...错误
- springmvc 出现No mapping found for HTTP request with URI
- 解决spring mvc No mapping found for HTTP request with URI错误的三种方法
- No mapping found for HTTP request with URI [/app17a/] in DispatcherServlet with name 'springmvc'解决方法
- No mapping found for HTTP request with URI [/chart/get] in DispatcherServlet with name 'SpringMVC‘
- No mapping found for HTTP request with URI [/SpringMvc2/ItemList] in DispatcherServlet with name 'sp
- 完美解决SpringMVC中静态资源无法找到(No mapping found for HTTP request with URI)问题
- springMVC 报404 No mapping found for HTTP request with URI [/ssmAngular/student/toIndex] in Dispatch
- No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'springmvc'
- SpringMVC - 问题. No mapping found for HTTP request with URI
- 解决spring mvc No mapping found for HTTP request with URI错误的三种方法
- Spring No mapping found for HTTP request with URI错误
- @Controler注解未生效报No mapping found for HTTP request with URI 错误
- No mapping found for HTTP request with URI [/user/login.do] in DispatcherServlet with name 'dispatcher'错误
- SpringMVC资源无法找到(No mapping found for HTTP request with URI)问题解决
- SpringMVC中静态资源无法找到(No mapping found for HTTP request with URI)的四种解决方式
- springMVC之No mapping found for HTTP request with URI 问题
- No mapping found for HTTP request with URI [/index.do] in DispatcherServlet with name 'SpringMVC‘
- SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI