org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request
控制台报了这么个错:org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/springmvc_response/js/jquery-2.1.0.min.js] in DispatcherServlet with name 'dispa
·
控制台报了这么个错:org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/springmvc_response/js/jquery-2.1.0.min.js] in DispatcherServlet with name 'dispatcherServlet’
解决办法:springmvc静态资源配置错误。
以下是详细说明:
我的web是这么配置的servlet-mapping中的url-pattern一开始配置的就是 / 所以说不是web的事,这样排除web的问题
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>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
jsp
<%--
Created by IntelliJ IDEA.
User: administrator
Date: 2020/3/5
Time: 19:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
<script>
//页面加载,绑定单击事件
$(function(){
$("#btn").click(function () {
$.ajax({
url:"user/testAjax",
contentType:"application/json;charset=UTF-8",
data:'{username:"haha",password:"123",age:11}',
dataType:"json",
type:"post",
success:function (data) {
}
});
});
});
</script>
</head>
<body>
<a href="user/testString">testString</a>
<br/>
<button id="btn">发送ajax的请求</button>
</body>
</html>
controller
package cn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testString")
public String testString(){
System.out.println("testString方法执行了...");
return "success";
}
@RequestMapping("/testAjax")
public void testAjax(@RequestBody String body){
System.out.println("testAjax方法执行了...");
System.out.println(body);
}
}
重点来了springmvc.xml,原因是我静态资源配置有问题
错误的:
<mvc:resources location="/js/**" mapping="/js/"/>
<mvc:resources location="/css/**" mapping="/css/"/>
<mvc:resources location="/images/**" mapping="/images/"/>
正确的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="cn"/>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--前端控制器,哪些静态资源不拦截-->
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<!--开启springMVC框架注解的支持-->
<mvc:annotation-driven />
</beans>
更多推荐
已为社区贡献5条内容
所有评论(0)