如何检查 ContextRelativeResource 是否存在? [英] How to check if ContextRelativeResource exists?
问题描述
在 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屋!