问题描述
我有一个名为Main.jsp的文件,位于
的绝对网址路径http://Mywebpage.com/Open/This/Folder/Main.jsp。
在Main.jsp内部,有一个jsp包含:
<%@ include file = ../../Top.jsp%>
现在,在Top.jsp页面中,我有其他jsp和javascript语句引用文件:
<%@ taglib uri =emonogram.tldprefix =em%>
...
< script type =text / javascriptsrc =HL.js>< / script>
emonogram.tld和HL.js存储在与Top.jsp相同的目录中,即http ://Mywebpage.com/Open/。
我需要Top.jsp足够灵活,这样任何文件都可以引用它,无论它在哪里目录树。这里的问题是我收到错误,因为无法找到Top.jsp中引用的文件。为什么? jsp包含路径将是Main.jsp的相对路径。所以,Top.jsp会失败因为,当我打电话给emonogram.tld时,我想要http://Mywebpage.com/Open/emonogram.tld,但它实际上是http://Mywebpage.com/Open /This/Folder/emonogram.tld。
我试过看一些jsp选项,比如getRequestURL,getServletPath,getRealPath和getContextPath,但这些方法看起来不像返回我想要的内容。
我当前的逻辑是检索Top.jsp的相对路径,并将其分别添加到emonogram.tld和HL.js。但我不知道该怎么做;我想弄明白。
更新根据BalusC的回答,我已经安装了标签和Tomcat 5.5。我按照链接和web.xml包含相应的信息。我甚至更新到JSP 1.2并没有。如果 $ {}
只相当于在< %%>
标记内执行,那么它不是一个成败的情况,在我得到原始问题后我可以担心它。但是谢谢你。
我跟着并尝试了以get ...开头的所有方法,但似乎没有一个方法可以解决问题。
getContextPath()
在BOTH Top.jsp和Main.jsp中给我 / Open
,即使Main.jsp在/ Open / This / Folder /中。这个问题正在影响加载emonogram.tld
,这是一个标记库,不仅仅是javascript文件。
Update2:我的道歉,拼写错误;我的意思是JSTL 1.2,而不是JSP 1.2。我在Tomcat 5.5.28,JSP 2.0和JSTL 1.2。
顺便说一句,感谢您的帮助和知识。它是相同的,即使不鼓励小脚本也是好的。使用 getcontextPath()
,我期待Main.jsp和<$ c的 / Open / This / Folder /
对于Top.jsp,$ c> / Open / 但对于两个文件都返回 / Open /
,这很奇怪。我会继续调查,希望能够解决问题,再次感谢你。
< script src>
与当前请求有关URL(如浏览器地址栏中所示),而不是JSP文件的服务器端位置。这就是需要加载脚本的web浏览器,而不是网络服务器。
因此,如果当前请求的URL是
并且JS文件实际位于
然后你需要引用它
< script type =text / javascriptsrc =/ HL.js>< / script>
前导斜杠将使其相对于域根。
但是,如果您的webapp本身未部署在域根目录上,而是在(过度简化的)示例中的上下文路径上,例如 / Open
,你的JS文件实际上位于
那么你需要使用。
< script type =text / javascriptsrc =$ {pageContext.request.contextPath} /HL.js>< / script>
这将最终为(在浏览器中右键单击页面,执行查看源看到它)
< script type =text / javascriptsrc =/ Open /HL.js\"></script>
参见:
更新:根据您的更新,请注意,这不适用于TLD文件,因为它们已在服务器端解析。通常,您应该将TLD文件放在 / WEB-INF
文件夹中,并通过 uri =/ WEB-INF / filename.tld
。
I have a file called Main.jsp, located at the absolute url path of"http://Mywebpage.com/Open/This/Folder/Main.jsp".
Inside Main.jsp, there is a jsp include:
<%@ include file="../../Top.jsp" %>
Now, inside the Top.jsp page, I have other jsp and javascript statements which reference files:
<%@ taglib uri="emonogram.tld" prefix="em" %>
...
<script type="text/javascript" src="HL.js"></script>
emonogram.tld and HL.js are stored in the same directory as Top.jsp, i.e. "http://Mywebpage.com/Open/".
I need Top.jsp to be flexible enough, such that any file can reference it, no matter where it is in the directory tree. The problem here is I'm getting errors because the files referenced in Top.jsp cannot be found. Why? The jsp include path will be the relative path of Main.jsp. So, Top.jsp will fail because, when I'm calling emonogram.tld, I want "http://Mywebpage.com/Open/emonogram.tld", but it is actually "http://Mywebpage.com/Open/This/Folder/emonogram.tld".
I tried looking at some jsp options such as getRequestURL, getServletPath, getRealPath, and getContextPath, but those methods don't seem to return what I want.
My current logic is to to retrieve the relative path of Top.jsp and prepend that to emonogram.tld and HL.js, respectively. But I don't know how to do that; I'm trying to figure that out.
Update: as per the answer of BalusC, I have tags installed and Tomcat 5.5. I followed the link and web.xml contains the appropriate info. I even updated to JSP 1.2 and nothing. If the ${}
is just the equivalent of doing it inside <% %>
tags, then it's not a make or break situation and I can worry about it after I get the original question working. But thank you.
I followed this link and tried all the methods starting with "get..." and none of them seemed to do the trick.
getContextPath()
gives me /Open
in BOTH Top.jsp and Main.jsp, even though Main.jsp is in /Open/This/Folder/. This problem is affecting loading "emonogram.tld"
, which is a tag library, not just javascript files, unfortunately.
Thank you again.
Update2: My apologies, big spelling error; I meant JSTL 1.2, NOT JSP 1.2. I am at Tomcat 5.5.28, JSP 2.0, and JSTL 1.2.
Thank you for your help and knowledge, by the way. It is the same, that is good, even though scriptlets are discouraged. With getcontextPath()
, I'm expecting /Open/This/Folder/
for Main.jsp and /Open/
for Top.jsp but it returns /Open/
for both files, which is quite strange. I'll continue investigating and hopefully arrive at a soltuion, thank you again.
The <script src>
is relative to the current request URL (as you see in browser address bar), not to the server side location of the JSP file. It's namely the webbrowser who needs to load the script, not the webserver.
So, if the current request URL is
and the JS file is actually located in
then you need to reference it as
<script type="text/javascript" src="/HL.js"></script>
The leading slash will make it relative to the domain root.
However, if your webapp is not deployed on the domain root per se, but on a context path, such as /Open
in your (oversimplified) example, and your JS file is actually located in
then you need to prepend the URL with HttpServletRequest#getContextPath()
.
<script type="text/javascript" src="${pageContext.request.contextPath}/HL.js"></script>
This will end up as (rightclick page in browser, do View Source to see it)
<script type="text/javascript" src="/Open/HL.js"></script>
See also:
- How to use relative paths without including the context root name?
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
Update: as per your update, please note that this does not apply on TLD files since they are been resolved on the server side. Normally, you should drop TLD files in /WEB-INF
folder and reference it by uri="/WEB-INF/filename.tld"
.
这篇关于检索包含文件jsp中文件的相对url路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!