Reputation: 15
In Wicket I add a new image to the page:
String filename = "images/specialLogo.jpg";
add(new Image("logoImage", new ContextRelativeResource(filename)));
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)
}
Upvotes: 1
Views: 311
Reputation: 1554
I tried this solution on my test page in Wicket project.
We can add a context to filename and that will be a full path to a file. So (as you need) if it's exists, we get it otherwise take another picture:
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)));
Upvotes: 1
Reputation: 17513
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.
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
Upvotes: 0