访问 Web 应用程序的静态资源

发布于 2024-10-27 21:17:47 字数 840 浏览 1 评论 0原文

我刚刚使用 Wicked 框架构建了一个非常简单的 Java Web 应用程序。项目的当前布局是:

src/
    main/
        java/
            net/myapp/
                Homepage.java
                Homepage.html
        reources/
            scripts/
                script.js

Homepage.html 文件中,我尝试加载 JavaScript 文件:

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

我部署了应用程序,但浏览器找不到 JavaScript 文件。

WAR 文件正在使用 maven-war-plugin 进行打包。我查看了 WAR 文件,看到以下布局:

WEB-INF/
    classes/
        net/myapp/
            Homepage.class
            Homepage.html
        scripts/
            script.js

我做错了什么?我缺少什么?

I've just built a very simple Java web application using the Wicked framework. The current layout of the project is:

src/
    main/
        java/
            net/myapp/
                Homepage.java
                Homepage.html
        reources/
            scripts/
                script.js

In the Homepage.html file I am trying to load the JavaScript file:

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

I deployed the application, but the browser doesn't find the JavaScript file.

The WAR file is being packaged using the maven-war-plugin. I looked into the WAR file, and I see the following layout:

WEB-INF/
    classes/
        net/myapp/
            Homepage.class
            Homepage.html
        scripts/
            script.js

What am I doing wrong? What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假面具 2024-11-03 21:17:47

Web相关资源应该放在src/main/webapp

The web-related resources should be placed in src/main/webapp

远昼 2024-11-03 21:17:47

您的目录结构应该是:

WEB-INF/
    classes/
        net/myapp/
            Homepage.class
            Homepage.html
        net/myapp/scripts/
            script.js

并且您的标记应该是:

<wicket:link><script src="scripts/script.js"></script></wicket:link>

Your directory structure should be:

WEB-INF/
    classes/
        net/myapp/
            Homepage.class
            Homepage.html
        net/myapp/scripts/
            script.js

and your markup should be:

<wicket:link><script src="scripts/script.js"></script></wicket:link>
烟织青萝梦 2024-11-03 21:17:47

WEB-INF 文件夹后面的资源不公开。如果 Homepage.class 转发到 Homepage.html 文件,您应该会看到没问题。但在 HTML 页面中,您可以引用 javascript 文件,该文件不公开。您需要将脚本移到 WEB-INF 之外。结构应该看起来像

WEB-INF /
     classes /
     net/myapp/
          Homepage.class
          Homepage.html
scripts/
      scripts.js

这样,html 文件中的引用才能

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

正常工作。当 HTML 页面渲染到用户端时,他们会回调以获取 JavaScript 资源。此时,该文件需要可见。

您的构建脚本或应用程序布局的更新应该会为您解决这个问题。

编辑:请参阅 Bozho 的回答,它将修复 Maven 的构建。请参阅此 Maven 链接

Resource sitting behind WEB-INF folder are not publicly available. If Homepage.class forwards to the Homepage.html, file you should be seeing that fine. But in the HTML page you have your reference to the javascript file, which is not publicly available. You need to move the scripts outside of the WEB-INF. The structure should look like

WEB-INF /
     classes /
     net/myapp/
          Homepage.class
          Homepage.html
scripts/
      scripts.js

This way a refernce in the html file to

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

will work properly. When the HTML page is rendered on the user side, they will make the call back to get the JavaScript resource. At this point, the file needs to be visible.

An update of your build script, or app layout should take care of this for you.

Edit: See Bozho's answer, it will fix the build for Maven. see This link for Maven

長街聽風 2024-11-03 21:17:47

虽然其他答案总体上是正确的,但他们并没有完全考虑 Wicket。使用 Wicket,您可以在类路径上拥有资源,并且在某些情况下它们比静态文件更好。

您可以使用 Application.mountSharedResource() 为共享资源分配一个 url,该资源可以来自任何地方,包括您的类路径。

While the other answers are correct in general, they don't quite take Wicket into account. With Wicket, you can have resources on the classpath, and in some cases they are better than a static file.

You can use Application.mountSharedResource() to assign a url to a shared resource, which can come from anywhere, your classpath included.

老娘不死你永远是小三 2024-11-03 21:17:47

不带 s 的拼写资源?

reources/

Spelling resources without the s?

reources/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文