Java JSP 包含指令、jsp:include 动作、相对路径与绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21837961/
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
JSP include directive, jsp:include action, relative vs. absolute paths
提问by Matt Mc
I am doing some basic templating in my JSP-based webapp. For example, I want to have a standard header and footer (basic HTML) that I pull into each of my JSPs.
我正在我的基于 JSP 的 web 应用程序中做一些基本的模板。例如,我想要将标准页眉和页脚(基本 HTML)拉入我的每个 JSP 中。
My content JSP is at /WEB-INF/jsp/home.jsp
, and I have template JSPs at /WEB-INF/jsp/template/
, such as /WEB-INF/jsp/template/Body-Footer.jsp
.
我的内容 JSP 位于/WEB-INF/jsp/home.jsp
,并且我的模板 JSP 位于/WEB-INF/jsp/template/
,例如/WEB-INF/jsp/template/Body-Footer.jsp
.
So now, within home.jsp
, I want to pull in my template files. First, I try the jsp:include
action:
所以现在,在 内home.jsp
,我想拉入我的模板文件。首先,我尝试以下jsp:include
操作:
<jsp:include page="template/Body-Footer.jsp"></jsp:include>
It generates the error javax.servlet.ServletException: File "/template/Body-Footer.jsp" not found
它产生错误 javax.servlet.ServletException: File "/template/Body-Footer.jsp" not found
Strange to me, considering that Eclipse says that the path is valid.
对我来说很奇怪,考虑到 Eclipse 说路径是有效的。
Okay, so then I switch to the include directive:
好的,那么我切换到包含指令:
<%@ include file="template/Body-Footer.jsp" %>
This works just fine, pulls in my footer HTML.
这工作得很好,拉入我的页脚 HTML。
But why does the jsp:include
not work? After some experimentation, I find that putting in the absolute path does get it to work:
但为什么不起作用jsp:include
?经过一些实验,我发现放入绝对路径确实可以让它工作:
<jsp:include page="/WEB-INF/jsp/template/Body-Footer.jsp"></jsp:include>
Now it works fine, no errors.
现在它工作正常,没有错误。
So here's my question: why? Why do I (apparently) need to use an absolute path with the jsp:include
action, but not with the include directive?
所以这是我的问题:为什么?为什么我(显然)需要在操作中使用绝对路径jsp:include
,而不是在 include 指令中使用?
采纳答案by developerwjk
/WEB-INF/jsp/template/Body-Footer.jsp
is not an absolute path. Its also a relative path. The problem is that template/Body-Footer.jsp
is an incomplete relative path, whereas the other is complete. That is, the paths are relative to your app path. Since /WEB-INF/
is under your app path, you have to include it. Absolute path means like C:/program files/tomcat/webapps/yourapp/WEB-INF/jsp/template/Body-Footer.jsp
/WEB-INF/jsp/template/Body-Footer.jsp
不是绝对路径。它也是一个相对路径。问题是这template/Body-Footer.jsp
是一个不完整的相对路径,而另一个是完整的。也就是说,路径是相对于您的应用程序路径的。由于/WEB-INF/
在您的应用程序路径下,您必须包含它。绝对路径意味着像C:/program files/tomcat/webapps/yourapp/WEB-INF/jsp/template/Body-Footer.jsp
回答by dejanmr
Answer to WHY - The jsp:include
is a runtime directive unlike the <%@ include ... %>
directive which happens to be a compile time directive (translation time, actually).
对 WHY 的回答 - 这jsp:include
是一个运行时指令,与<%@ include ... %>
恰好是编译时指令(实际上是翻译时间)的指令不同。
See more on: JSP Performance using jsp:include
查看更多关于:使用 jsp:include 的 JSP 性能
Bottom line - directives are run against different folders as a base.
底线 - 指令针对不同的文件夹作为基础运行。
Btw. JSP pages should be outside of WEB-INF folder, if you want to follow official recommendation:
顺便提一句。JSP 页面应该在 WEB-INF 文件夹之外,如果你想遵循官方建议:
回答by gavenkoa
I read JSP 2.0 spec and here:
我阅读了 JSP 2.0 规范和这里:
Relative URL Specifications
* A context-relative path is a path that starts with a slash (/).
It is to be interpreted as relative to the application to which
the JSP page or tag file belongs. That is, its ServletContext
object provides the base context URL.
* A page relative path is a path that does not start with a
slash (/). It is to be in- terpreted as relative to the current
JSP page, or the current JSP file or tag file, depending on where
the path is being used.
For now javax.servlet.ServletContext.getRealPath("/WEB-INF/test/test.jsp")
is null for security reason.
出于javax.servlet.ServletContext.getRealPath("/WEB-INF/test/test.jsp")
安全原因,现在为空。
Assume that context-relative pathis path from your WAR root.
假设上下文相关路径是来自您的 WAR 根的路径。