Celahirius
Celahirius

Reputation: 15

How to check if ContextRelativeResource exists?

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

Answers (2)

sanluck
sanluck

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

Related Questions