java pageContext.request.contextPath 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5877354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:14:33  来源:igfitidea点击:

pageContext.request.contextPath not working

javajsptomcatcontextpath

提问by arturopuente

I'm using Tomcat 6.0.32 and I'm trying to include a JSP file into another, but somehow the file cannot be found (yes, the file exists). Here is the file structure and code:

我正在使用 Tomcat 6.0.32 并且我试图将一个 JSP 文件包含到另一个文件中,但不知何故无法找到该文件(是的,该文件存在)。这是文件结构和代码:

<jsp:include page="${pageContext.request.contextPath}/templates/header.jsp">
</jsp:include>

WebContent
- folder/caller.jsp
- templates/header.jsp

However, if I use ${pageContext.request.contextPath} outside of that to check if it outputs correctly, it works, does anyone know why this is happening?

但是,如果我在此之外使用 ${pageContext.request.contextPath} 来检查它是否正确输出,它可以工作,有人知道为什么会这样吗?

回答by matt b

I'm not even sure if you can use runtime variables inside a <jsp:include>, but the other problem is that the contextPathrefersto the context of the URL being requested from the server, and does not necessarily correspond to your filesystem layout.

我什至不确定您是否可以在 a 中使用运行时变量<jsp:include>,但另一个问题是thecontextPath的是从服务器请求的 URL 的上下文,并且不一定对应于您的文件系统布局。

Why wouldn't you just use <jsp:include page="templates/header.jsp">?

你为什么不直接使用<jsp:include page="templates/header.jsp">

回答by pradks

i completely agree with the above answer however if u have to use it this way then the below code shd work

我完全同意上面的答案,但是如果你必须这样使用它,那么下面的代码 shd 工作

<c:set var="myContext" value="${pageContext.request.contextPath}"/>
<jsp:include page="${myContext}/templates/header.jsp"> </jsp:include>