jsp中獲取項目根路徑:
方法①
最頂部增加代碼:
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
然后在head標簽增加代碼:
<base href="<%=basePath%>">
結果為:http://localhost:8080/MyWeb/
方法②
只在head標簽引入JSTL表達式
<base href="${pageContext.request.contextPath}">
結果為:/MyWeb
idea下使用方法①有些情況下,例如maven下會報錯,可以點此查看解決方法