在jsp,html中使用的地址,都是在前端页面中的地址,都是相对地址
地址分类:
1.绝对地址,带有协议名称的是绝对地址例如http://www.baidu.com
2.相对地址,没有协议开头的,例如 user/some.do,
相对地址不能独立使用,必须有一个参考地址。通过参考地址+相对地址本身才能指定资源。
3.参考地址
1)在你的页面中的,访问地址不加"/"
访问的是http://localhost:8080/SpringMVC/index.jsp
当前页面的地址:http://localhost:8080/SpringMVC/
资源:index.jsp
在index.jsp中发起user/some.do请求,访问地址变为http://localhost:8080/SpringMVC/user/some.do,当你点击链接时,访问地址就是当前页面的地址加上链接的地址。
http://localhost:8080/SpringMVC/ + user/some.do
如果访问的是http://localhost:8080/SpringMVC/user/some.do
路径:http://localhost:8080/SpringMVC/user/
资源:some.do
此时再在该页面里发起user/some.do请求,访问地址就会变为http://localhost:8080/SpringMVC/user/user/some.do
解决方案:
1.加入${pageContext.request.contextPath}使用绝对路径
2.加入一个base标签,是html语言中的标签。表示当前页面中访问地址的基地址。你的页面中所有没有“/”开头的地址,都是以base标签中的地址作为参考地址的
<head>
<title>Title</title>
<base href="http://localhost:8080/SpringMVC/">
</head>
如果担心服务器、端口这些改变了会导致base中的路径需要重新设置,那么可以这样写,当然,仅限于jsp
<%
String basePath=request.getScheme()+"://"+
request.getServerName()+":"+request.getServerPort()+
request.getContextPath()+"/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
2)在你的页面中,访问地址加"/"
访问的是http://localhost:8080/SpringMVC/index.jsp
路径:http://localhost:8080/SpringMVC/
资源:index.jsp
在idnex.jsp中发起/user/some.do请求,访问地址变为http://localhost:8080/user/some.do,参考地址是你的服务器地址http://localhost:8080
此时无法访问,那么我们可以在访问时将路径定义为/SpringMVC/user/some.do,不过这样也不够灵活,当后端的项目映射名改变时,需要修改所有的前端访问路径,如果是jsp,此时可以使用el表达式
<a href="${pageContext.request.contextPath}/user/some.do"></a>
发起user/some.do的get请求