- Foreword: Yesterday I finished building the framework of the spring mvc project, but I found that I can't access the page anymore. After carefully looking at the code, I found that: When the path jumps, I added an extra / slash in front of the path, which caused the requested path to occur Error, let’s share with you about the path forward slash problem
1. About the request path:
Only a correct request path can access the resources of the server, the request path is composed of the resource path and the resource name
例如: http://localhost:8080/springmvc/test/hello
Where http://localhost:8080/springmvc/test is the resource path and hello is the resource name
2. Request path classification:
The request path is divided into front-end path and back-end path . The path in the jsp page is the front-end path. If the path in .xml can be said to be the back-end path, this time I mainly introduce the front-end path, and the front-end path will be divided into Two types: relative path and absolute path.
3. The request path is divided into relative path and absolute path:
For example: http://localhost:8080/springmvc/test/hello is an absolute path (full path), which can accurately locate a resource
For example: /test/hello or test/hello are relative paths, they will rely on one other path as the most reference path
If you look carefully, you will find: Why is there a slash (/) in front of the same relative path? This is the main content shared in this blog.
- If the path is resolved in the foreground, the root path is http://localhost:8080/
- If the path is resolved in the background, the root path is http://localhost:8080/project name/
Look at an example:
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序</h3>
<!--
当前路径是前面没有斜杠的相对路径,参照的路径就是当前页面的路径:
参照的路径是http://localhost:8008/项目名/requestMapping
http://localhost:8008/test/requestMapping
-->
<a href="requestMapping">前面没有斜杠</a>
<!--当前路径是一个有斜杠的相对路径,参照路径就是当前的web服务器的根路径:http://localhost:8008/
点击链接之后提交的URL绝对地址是: http://localhost:8008/requestMapping
-->
<a href="/requestMapping">前面有斜杠</a>
</body>
</html>
Controller:
*/
@Controller
public class HelloController {
@RequestMapping(path="/requestMapping")
public String testRequestmapping(){
System.out.println("hello spring mvc");
return "success";
}
}
Initial page:
When we click on the first hyperlink, there is no slash in front of the relative path:
The URL in the address bar becomes:
The green part in the front is the reference path of the relative path (ie http://localhost:8008/test/haha/), which is the path before we clicked on the link.
When we click on a link with a slash in front:
The URL becomes:
We can find that the reference path of the relative path has become the root directory (ie http://localhost:8008/)