Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 743
Tony Avatar asked Sep 24 '13 14:09

Tony


1 Answers

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']}");
// ...
like image 187
BalusC Avatar answered Sep 29 '22 08:09

BalusC