访问 Web 应用程序的静态资源
我刚刚使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Web相关资源应该放在
src/main/webapp
中The web-related resources should be placed in
src/main/webapp
您的目录结构应该是:
并且您的标记应该是:
Your directory structure should be:
and your markup should be:
WEB-INF 文件夹后面的资源不公开。如果 Homepage.class 转发到 Homepage.html 文件,您应该会看到没问题。但在 HTML 页面中,您可以引用 javascript 文件,该文件不公开。您需要将脚本移到 WEB-INF 之外。结构应该看起来像
这样,html 文件中的引用才能
正常工作。当 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
This way a refernce in the html file to
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
虽然其他答案总体上是正确的,但他们并没有完全考虑 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.不带 s 的拼写资源?
Spelling resources without the s?