JSP绝对路径 [英] JSP absolute paths
问题描述
有人可以解释为什么不建议在JSP中使用绝对路径(例如,IntelliJ IDEA向我显示警告)?
Could someone explain why absolute paths not recommended to use in JSP (e.g., IntelliJ IDEA show me a warning)?
推荐答案
在JSP中考虑以下代码:
Consider the following code in your JSP:
<script src="/path/to/script.js" />
您在 www.example.com $ c上部署应用程序$ c>在servlet上下文
myContext
中,您的脚本将由浏览器查找
And you deploy your application on www.example.com
in servlet context myContext
, your script will be looked up by the browser in
www.example.com/path/to/script.js
但是,浏览器找不到脚本。实际可以找到它的URL包含servlet上下文以及URL的一部分:
However, the browser will not find the script. The URL where it can actually be found containts the servlet context as well as part of the URL:
www.example.com/myContext/path/to/script.js
因此,您应该将JSP中的URL更改为:
So you should change the URL in your JSP to:
<script src="${pageContext.request.contextPath}/path/to/script.js" />
然后,URL中也提供了上下文路径,一切正常。
Then the context path is also available in the URL and everything will work fine.
这篇关于JSP绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!