Tony
Tony

Reputation: 2406

How to get resource map programmatically in JSF?

I want to access the resource map, not from my CCS file as in

background-image: url("#{resource['primefaces-supertheme:images/ui-icons_ffffff_0.png']}");

but from my bean. Is it possible with EL evaluation only?

Upvotes: 6

Views: 2455

Answers (1)

BalusC
BalusC

Reputation: 1109695

The true Java variant would be Application#createResrouce() and then Resource#getRequestPath():

FacesContext context = FacesContext.getCurrentInstance();
Resource resource = context.getApplication().getResourceHandler().createResource("images/ui-icons_ffffff_0.png", "primefaces-supertheme");
String url = resource.getRequestPath();
// ...

Note that you could just evaluate EL programmatically. You can use Application#evaluateExpressionGet() for this.

FacesContext context = FacesContext.getCurrentInstance();
String url = context.getApplication().evaluateExpressionGet(context, "#{resource['primefaces-supertheme:images/ui-icons_ffffff_0.png']}", String.class);
// ...

If you happen to use JSF utility library OmniFaces, this can be simplified via Faces utility class as:

String url = Faces.evaluateExpressionGet("#{resource['primefaces-supertheme:images/ui-icons_ffffff_0.png']}");
// ...

Upvotes: 8

Related Questions