当前位置: 代码迷 >> 综合 >> No mapping found for HTTP request with URI [...] in DispatcherServlet with name ‘DispatcherServlet‘
  详细解决方案

No mapping found for HTTP request with URI [...] in DispatcherServlet with name ‘DispatcherServlet‘

热度:78   发布时间:2024-02-04 11:52:57.0

该错误出现在SpringMVC中,利用ajax和json获取集合类型参数时产生的。

 

代码

由jsp页面产生数据,并传递给Controller层。

<head><title>Title</title><script src="${pageContext.request.contextPath}/js/jquery-3.3.1.js"></script><script>var userList = new Array();userList.push({username:"zhangsan",age:18});userList.push({username:"lisi",age:28});$.ajax({dataType:'json',type:"POST",url:"${pageContext.request.contextPath}/user/quick14",data:JSON.stringify(userList),contentType:"application/json;charset=UTF-8"});</script>
</head>

接收:

@Controller
public class UserController {@RequestMapping("/quick2")@ResponseBodypublic void save2(@RequestBody List<User> userList){System.out.println(userList);}
}

spring-mvc.xml中:

<context:component-scan base-package="nuc.ss.Controller"/><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- /jsp/success.jsp --><property name="prefix" value="/jsp/"></property><property name="suffix" value=".jsp"></property></bean><mvc:resources mapping="/js/**" location="/js/"/>

 

注意点

①如果在Controller层的对应方法上,没有写 @ResponseBody ,那么就会直接导致该错误。
②注意扫描注解时,对应的包一定要正确。
③一定要加载静态资源 <mvc:resources mapping="/js/**" location="/js/"/>

 

  相关解决方案