如何访问 WAR 的根目录?

发布于 2024-07-18 03:32:48 字数 88 浏览 9 评论 0原文

我需要确定 JSP 标记中是否存在图像,以便在不存在时显示默认值。 访问 War 根目录以便我可以检查图像是否存在的最佳方法是什么? 或者有更好的解决方案吗?

I need to determine if an image exists in a JSP tag so I can display a default if it doesn't exist. What is the best way to access the War root so that I can check to see if the image exists? Or is there a better solution?

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

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

发布评论

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

评论(2

水染的天色ゝ 2024-07-25 03:32:48

ServletContext 方法 getResource 可用于此类功能。 如果资源(图像)不存在,此方法返回null。 在这种情况下,可以显示默认图像。

请注意,此方法返回的 URL 不是用作 标记中的“src”属性的 URL。 它很可能是一个在其他主机上没有意义的 file:jar: 协议 URL。 在此应用程序中,仅测试其是否为 null,而不是实际由 JSP 进行测试。

The ServletContext method getResource can be used for features like this. If the resource (the image) doesn't exist, this method returns null. In that case, a default image can be displayed.

Note that the URL this method returns is not the URL to use as the "src" attribute in an <img> tag. It is most likely a file: or jar: protocol URL that wouldn't make sense on another host. In this application, its only tested to see if it is null, and not actually by the JSP.

才能让你更想念 2024-07-25 03:32:48

另请注意,无法保证 Web 容器已分解您的 WAR 文件,因此您应该避免查看文件系统。

是否可以选择在部署时生成可用映像列表?

Also note there is no guarantee that the web container has exploded your WAR file, so you should avoid peeking at the filesystem.

Would it be an option to generate the list of available images at deployment time?

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