The addresses we use in jsp and html are all addresses in the front-end page. Such addresses that do not start with the protocol are relative addresses:
We use domain names to access web pages on the Internet, such as: https://www.baidu.com . This type of address with a protocol name is an absolute address.
The relative address in SpringMVC will be accessed through the current address (reference address) + relative address splicing out the absolute address:
1. The relative address does not start with /:
After the project is started, the address bar url of the browser is our reference address: http://localhost:8080/SpringMVC_01/ , the relative address is spliced to get the absolute address:
2. Relative addresses beginning with /
We found that the reference address of the project became http://localhost:8080 at this time , and the resource path accessed after splicing was wrong. In fact, the relative address that starts with /, the reference address he uses is the host name, missing the project name; if you must use this method, you need to add the project name (using EL expressions to dynamically obtain):
3. Comparison of advantages
Although it seems simple to not start with /, it also has disadvantages. We don't use the view parser and let the page jump back to index.jsp:
@RequestMapping(value = "/some.do" , method = RequestMethod.GET)
public ModelAndView doSome(){
ModelAndView modelAndView = new ModelAndView();
// 存放数据 框架会自动将数据放到request作用域
modelAndView.addObject("msg","hello-get");
modelAndView.setViewName("/index.jsp");
return modelAndView;
}
<?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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="com.zzt.Controller"/>
<!--配置视图解析器-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>-->
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--静态资源处理 方式1
<mvc:default-servlet-handler/>-->
<!--静态资源处理 方式2-->
<mvc:resources mapping="/static/**" location="/static/" />
</beans>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="test/some.do">发起some.do请求</a>
</body>
</html>
Note that SpringMVC uses the forward method, so the URL will not change with the jump. At this time, the address can be parsed as follows:
Reference path: http://localhost:8080/SpringMVC_02/test/
Source: some.do
What if we make another request at this time?
We use a method that does not start with /, but when we initiate the second request, the reference path is different from the first one. Through splicing, add it after http://localhost:8080/SpringMVC_02/test/ The relative address of the resource, test/some.do , is http://localhost:8080/SpringMVC_02/test/test/some.do , which obviously has no mapping to it, nor is it a static resource.
Solution 1: Add dynamic project name
<a href="${pageContext.request.contextPath}/test/some.do">发起some.do请求</a>
Solution 2: Use the <base> tag in html to fix the base address. All relative addresses that do not start with / will use the base address as the reference address
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<base href="http://localhost:8080/SpringMVC_02/">
</head>
<body>
<a href="test/some.do">发起some.do请求</a>
</body>
</html>
Of course, it is impossible for us to write the base address of a base tag separately for each page. Therefore, we can also dynamically obtain the project address in jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme()+ "://" +
request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>" />
</head>
<body>
<a href="test/some.do">发起some.do请求</a>
</body>
</html>