如何检查ContextRelativeResource是否存在?

时间:2016-01-14 20:32:51

标签: java wicket

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

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

如何检查此“specialLogo.jpg”文件是否存在,方法是在之前添加文件名是放置应用程序.war文件的正确路径(ContextRelative)? 换句话说:怎么做:

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

2 个答案:

答案 0 :(得分:1)

我在Wicket项目的测试页面上尝试了这个解决方案。

我们可以为文件名添加上下文,这将是文件的完整路径。 所以(如你所知)如果它存在,我们得到它,否则拍另一张照片:

String context = ((WebApplication)Application.get()).getServletContext().getContextPath();
String filenameSpecial = "/images/specialLogo.jpg";
String filenameNormal = "/images/normalLogo.jpg";
File f = new File(context + filenameSpecial);
add(new Image("logoImage", new ContextRelativeResource(f.exists() ? filenameSpecial : filenameNormal)));

答案 1 :(得分:0)

相关问题