简体   繁体   English

如果我在Jersey的@Path注释中使用子资源,则找不到JavaScript文件

[英]JavaScript file not found if I use sub-resource in Jersey's @Path annotation

I have a Jersey REST web service and here is my resource class for the URL path http://localhost:8080/myapp/account/home . 我有一个Jersey REST Web服务,这是URL路径http://localhost:8080/myapp/account/home资源类。

@Path("account/home")
public class AccountResources {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable displayForm() {
        return new Viewable("/homepage.html");
    }
}

The HTML file homepage.html is linked with a JavaScript file which should be loaded from js/jump.js on the server: HTML文件homepage.html与一个JavaScript文件链接,该文件应从服务器上的js/jump.js加载:

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

However, when I launched my webapp and opened http://localhost:8080/myapp/account/home (basically this is homepage.html ), the JavaScript file js/jump.js is not loaded. 但是,当我启动Webapp并打开http://localhost:8080/myapp/account/home (基本上是homepage.html )时,未加载JavaScript文件js/jump.js So I had to add the context root path /myapp/ to make it work: 因此,我必须添加上下文根路径/myapp/才能使其工作:

<script src="/myapp/js/jump.js"></script>

This is not pretty since the context root path /myapp can be changed later. 这不是很漂亮,因为上下文根路径/myapp可以在以后更改。 So I don't want to add /myapp everywhere in my HTML files. 因此,我不想在我的HTML文件中的任何地方添加/myapp Also, the physical directory layout in my webapp is as follows: 另外,我的webapp中的物理目录布局如下:

WebContent
    |
    |_____ homepage.html
    |
    |_____ js
            |
            |____ jump.js

So why it doesn't work if I use <script src="js/jump.js"></script> in homepage.html even though physically js/jump.js and homepage.html are in the same web root directory on the server? 因此,即使实际上js/jump.jshomepage.html在相同的Web根目录中,即使我在homepage.html使用<script src="js/jump.js"></script> ,为什么它不起作用服务器? Does Jersey servlet change the relative URL path of HTML and JavaScript files if I use @Path annotation in that way? 如果我以这种方式使用@Path注释,Jersey servlet是否会更改HTML和JavaScript文件的相对URL路径? I tried another way of using @Path without subresource: 我尝试了使用@Path而不使用子资源的另一种方式:

@Path("account")

This time I can use <script src="js/jump.js"></script> in my homepage.html and the JS file can be loaded successfully if I open http://localhost:8080/myapp/account . 这次,我可以在homepage.html使用<script src="js/jump.js"></script> ,如果打开http://localhost:8080/myapp/account则可以成功加载JS文件。 So it seems that subresource in the URL path defined in Jersey's @Path("account/home") annotation will make the JavaScript relative file path unsearchable. 因此,似乎在Jersey的@Path("account/home")批注中定义的URL路径中的子资源将使JavaScript相对文件路径不可搜索。

Any good way to make both happy? 有什么好办法可以让双方都开心吗? (ie, I can use both subresource in URL path like @Path("account/home") without changing js/jump.js to /myapp/js/jump.js ) (即,我可以在URL路径中使用两个子资源,例如@Path("account/home")而无需将js/jump.js更改为/myapp/js/jump.js

Try writing 尝试写作

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

On your first example the server evaluates the relative path to http://localhost:8080/myapp/account/js/jump.js 在第一个示例中,服务器评估到http://localhost:8080/myapp/account/js/jump.js的相对路径

but what you need is: 但您需要的是:

http://localhost:8080/myapp/js/jump.js

So just let the browser know to look up one path up. 因此,只要让浏览器知道查找一个路径即可。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM