支持JSTL標簽庫和EL表達式的兩個jar包
1.用JSTL標簽庫和EL表達式
引入JSTL標簽庫
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
JSTL標簽
<c:set var="path" value="${pageContext.request.contextPath}"/>
JavaScript
<script type="text/javascript" src="${path}/statics/js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> var path = "${path}"; alert(path); </script>
谷歌F12中可以看到引入JavaScript的路徑已經變了
alert彈窗
與 <%=request.getContextPath()%>等價
2.basePath
JSP中Java代碼
<% String basePath = request.getScheme() + "://" + request.getServerName() + ":"+request.getServerPort() + request.getContextPath() + "/"; %>
JavaScript
<script type="text/javascript" src="${path}/statics/js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> alert("<%=basePath%>"); </script>
彈窗
解釋
request.getScheme() 返回協議的名稱 http,和后面的"://" 拼起來就成了 http://
request.getServerName() 這是獲取你的服務器的名稱,如果你的應用部署在本機那么其就返回localhost或者127.0.0.1 ,這2個是等價的
request.getServerPort() 是你應用使用的端口,比如8080或者80等等
3.<base href="<%=basePath%>">
<base> 標簽為頁面上的所有鏈接規定默認地址或默認目標
要在<head></head>下邊定義
沒有定義時候點擊跳轉
定義之后