如何访问 WAR 的根目录?
我需要确定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 returnsnull
. 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 afile:
orjar:
protocol URL that wouldn't make sense on another host. In this application, its only tested to see if it isnull
, and not actually by the JSP.另请注意,无法保证 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?