如何检查 ContextRelativeResource 是否存在? [英] How to check if ContextRelativeResource exists?

查看:36
本文介绍了如何检查 ContextRelativeResource 是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Wicket 中,我向页面添加了一个新图像:

In Wicket I add a new image to the page:

String filename = "images/specialLogo.jpg";
add(new Image("logoImage", new ContextRelativeResource(filename)));

如何通过在文件名之前添加 应用程序 .war 文件所在的正确路径 (ContextRelative) 来检查此specialLogo.jpg"文件是否存在?
换句话说:怎么做:

How can I check whether this "specialLogo.jpg" file exists, through adding before the filename a correct path where the application .war file has been placed (ContextRelative)?
In other words: how to do:

if (exists) {
  add...(specialLogo)
} else {
  add... (normalLogo)
}

推荐答案

一种方法是使用 https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResource(java.lang.String) 并检查非空结果.

One way is to use https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResource(java.lang.String) and check for non-null result.

另一种方法是https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String) 然后使用文件/路径 API

Another way is https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String) and then with the File/Path APIs

这篇关于如何检查 ContextRelativeResource 是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆