从 javascript 代码获取服务器上下文路径中的图像
我有一个基于 Spring 的 Web 应用程序 MyWebapp,使用 maven 构建并部署在 Websphere 6.1 上。
文件夹结构为:
MyApp -->源代码 ---> main --->
main 文件夹还包含 resources 和 webapp 文件夹。
webapp 文件夹包含其他文件夹,例如 images、theme、jscript、JSP、META-INF、WEB-INF
images 文件夹包含 icons 文件夹,其中包含 example.png
因此,在本地主机上获取 example.png
http://localhost:9080/MyWebapp/images/icons/example.png
成功:
在 jscript 文件夹中,我有一个 sample.js javascript 文件,其中定义了一些函数。
我在 JSP 页面中导入这个 javascript 文件,如下所示:
<script src="<%=request.getContextPath()%>/jscript/sample.js" type="text/javascript" language="Javascript"></script>
这个 javascript 文件有一个尝试获取图像的函数,如下所示:
iconFile = '../images/icons/search_result.png';
anchor.style.backgroundImage = 'url(' + iconFile + ')';
anchor.style.backgroundRepeat = 'no-repeat';
anchor.style.backgroundPosition = '1px 2px';
anchor.className = 'toplevel-tab';
完整的函数基本上尝试在 JSP 中的某些文本之前放置一个图标。
代码被解析。但是,图像不会显示。
在简单的 html 上独立运行代码(其中 png 图像与 html 和 javascript 文件位于同一文件夹中)会成功。这里我只有 iconFile = "search_result.png"
所以,这不是代码问题。
问题是图像未定位或服务器无法在上述 javascript 代码中找到图像。
我做错了什么? 我该如何解决它?
我之前接受的 https://stackoverflow.com/a/8298652/887235 的答案不起作用。 因此,请不要将此问题视为重复问题而否决。
此外,我正在受限制的环境中工作,在该环境中无法访问 Tail 等程序。
改成
iconFile = '../images/icons/search_result.png';
也
iconFile = '/images/icons/search_result.png';
行不通!!
感谢您的阅读!
I have a spring based web application MyWebapp built using maven and deployed on Websphere 6.1
The folder structure is:
MyApp --> src ---> main --->
The main folder is further having resources and webapp folders.
webapp folders is having other folders like images, theme, jscript, JSP, META-INF, WEB-INF
images folder is having icons folder with say example.png
So fetching example.png on localhost as:
http://localhost:9080/MyWebapp/images/icons/example.png
succeeds.
In jscript folder I have a sample.js javascript file where some functions are defined.
I am importing this javascript file in JSP pages as:
<script src="<%=request.getContextPath()%>/jscript/sample.js" type="text/javascript" language="Javascript"></script>
This javascript file is having a function which tries to fetch image as below:
iconFile = '../images/icons/search_result.png';
anchor.style.backgroundImage = 'url(' + iconFile + ')';
anchor.style.backgroundRepeat = 'no-repeat';
anchor.style.backgroundPosition = '1px 2px';
anchor.className = 'toplevel-tab';
The complete function basically tries to place a icon before some text in JSP.
The code gets parsed. However, the image does not get displayed.
Running the code independently on a simple html with the png images in the same folder as html and javascript files succeeds. Here i will just have iconFile = "search_result.png"
So, it is not code issue.
Issue is that the image is not getting located or the server is unable to find the image in above javascript code.
What am I doing wrong ?
How can I solve it ?
The answer for https://stackoverflow.com/a/8298652/887235 which I accepted earlier does not work.
So please do not downvote this question as a duplicate one.
Also I am working on restricted environment where access to programs like Tail will not work.
Changing
iconFile = '../images/icons/search_result.png';
to
iconFile = '/images/icons/search_result.png';
also does not work!!
Thanks for reading!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要了解相对路径是如何工作的。即使路径位于 JavaScript 文件中,该路径也不是相对于该 JS 文件的位置,而是相对于浏览器中显示的 HTML 页面的 URL。
因此,如果执行此 javascript 代码的页面的 URL
为 且图像的 URL
为 图像的绝对 URL 将
相同
与
/images/icons/search_result.png 也是从 Web 服务器的根目录解析的,而不是从 Web 应用程序的根目录解析的(浏览器不知道 Java Web 应用程序是什么,也不关心)。所以它被解决了,因为
您需要将上下文路径添加到路径中以使其真正绝对:
或者,没有 scriptlet:
You just have to understand how relative paths work. Even if the path is in a JavaScript file, the path is not relative to the location of this JS file, but it's relative to the URL of the HTML page being displayed in the browser.
So, if the URL of the page executing this javascript code is
and the URL of the image is
The absolute URL of the image will be
which is the same as
An absolute path like
/images/icons/search_result.png
is also resolved from the root of the web server, and not the root of the webapp (the browser doesn't know what a Java webapp is and doesn't care). So it's resolved asYou would need to prepend the context path to the path to make it really absolute:
or, without scriptlets:
您需要让 JavaScript 了解应用程序根目录的路径,因为这会根据上下文而改变。首先声明一个全局变量,如:
然后,就可以稍后使用它了,如:
You need to give your javascript an awareness of the path to the root of your application, as this will change on context. Start by declaring a global variable, such as:
Then, you are ready to use it later, such as: