开发者

jQuery scripts are not downloaded when the index page is opened on context root

开发者 https://www.devze.com 2023-03-17 03:35 出处:网络
I have a web application which uses jQuery UI. The JavaScripts are not downloaded and applied when I browse to the context root directly and rely on the welcome file:

I have a web application which uses jQuery UI. The JavaScripts are not downloaded and applied when I browse to the context root directly and rely on the welcome file:

http://localhost:8080/projectName/

But the JavaScripts are downloaded and applied when I browse to the welcome file:

http://localhost:8080/projectName/myFiles/index.html

My project structure is as below:

projectName
`-- WebRoot
    |-- myFiles
    |   `-- index.html
    `-- WEB-INF
    开发者_开发百科    `-- web.xml

In web.xml, I have defined as welcome file the index.html which uses jQuery UI.

<welcome-file-list>
    <welcome-file>myFiles/index.html</welcome-file>
</welcome-file-list>


This can happen if the path to the script source is relative to the current request URL, such as for example

<script src="jquery.js"></script>

When you open the page by

http://localhost:8080/projectName/

then it will attempt to download the script from

http://localhost:8080/projectName/jquery.js

When you open the page by

http://localhost:8080/projectName/myFiles/index.html

then it will attempt to download the script from

http://localhost:8080/projectName/myFiles/jquery.js

You would like to use a domain-relative path instead. Assuming that the file is indeed located in

http://localhost:8080/projectName/myFiles/jquery.js

then you need to declare the script source as follows

<script src="/projectName/myFiles/jquery.js"></script>

or, with dynamic resolving of the context path as this is subject to changes outside control from inside your webapp's code

<script src="${pageContext.request.contextPath}/myFiles/jquery.js"></script>
0

精彩评论

暂无评论...
验证码 换一张
取 消