springmvc 双亲上下文导致的 No mapping found for HTTP request

mac2022-06-30  25

今天搭建spring mvc ,结果发出请求总是No mapping found for HTTP request with URI [******]

于是开始排查了半天,后来在网上搜到了双亲上下文的概念,

才知道springmvc的每一个DispatcherServlet都会产生一个WebApplicationContext(子上下文),

它与Spring初始化生成的WebApplicationContext(父上下文)同时保存在ServletContext中

而所有的controller对象必须存放在springmvc的子上下文中,否则无法找到:No mapping found for HTTP request。

在父上下文中(applicationContext.xml),将Controller的注解排除掉  <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>

在springMVC配置文件中只扫描controller

<context:component-scan base-package="org.apollo.controller">   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>

 

而且如果父上下文中的service配置了事务,则子上下文中不能在扫描service对象,否则会产生service事务失效。

 

转载于:https://www.cnblogs.com/DajiangDev/p/3967874.html

最新回复(0)