${pageContext.request.contextPath }无法解析
${pageContext.request.contextPath}
用于解决使用相对路径时出现的问题 ,是JSP中取得绝对路径的方法,等价于<%=request.getContextPath()%> (${}EL表达式)
作用:取出部署的应用程序名,即获取当前的项目的名称
💡拓展:相对路径里常使用 “. ./” 来表示上一级目录。如果有多个上一级目录,可以使用多个“. ./”
解决问题(两个方面):
1.没有导入servlet和jsp的jar依赖包
pom.xml
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--JSP-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
2.wep-app 的版本要在2.4以上 才能识别EL
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
🌼jq获取当前文件目录
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="../js/jquery-3.6.0.min.js"></script>
<script >
$(function (){
function get_url(){
//获取当前网址,如: http://localhost:8083/proj/meun.jsp
var curWwwPath = window.document.location.href;
//获取主机地址之后的目录,如: proj/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPath = curWwwPath.substring(0, pos);
//获取带"/"的项目名,如:/proj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/')+1);
// return(localhostPath + projectName);
console.log(localhostPath + projectName);
return(localhostPath+projectName);
}
var url = get_url();
$("input").val(url)
})
</script>
</head>
<body>
<input type="text" style="width: 50%">
</body>
</html>
效果: