Reputation: 291
I am trying to get an image from resource like this:
<img src="#{resource['img/bla.svg']}"/>
It is working like this...
but now I want to generalize this file, so it will come from a java class like this:
LogoHandler.getLogo(String type)
I tried to put them together like this:
<img src="#{resource["#{logoHandler.getLogo("A1")}"]}"/>
but of course it is not working, just to show you what i want to achive.
Would you help me please?
Upvotes: 0
Views: 162
Reputation: 1108537
<img src="#{resource["#{logoHandler.getLogo("A1")}"]}"/>
EL indeed doesn't work like that. The #{...}
cannot be nested. The #{...}
actually represents a single scope wherin you can let multiple variables and expressions interact with each other, producing a single outcome. You should remove the inner #{...}
and make sure that the quotes doesn't clash.
Like as HTML and JavaScript, EL also supports single quotes. You can use single quotes instead of double quotes for the inner quotations.
<img src="#{resource[logoHandler.getLogo('A1')]}"/>
Else you'd have to escape the double quotes which makes the expressions harder to read.
<img src="#{resource[logoHandler.getLogo(\"A1\")]}"/>
Moreover, you're for this also dependent on the EL implementation being used. The single-slash escape only works in Oracle EL, while in Apache EL you need two. So your code would not be portable across servers using different EL implementations.
<img src="#{resource[logoHandler.getLogo(\\"A1\\")]}"/>
A completely different way is just using the JSF-provided <h:graphicImage>
component. It generates a HTML <img>
element. Its name
attribute represents the resource path. This way you don't anymore need the #{resource}
mapping.
<h:graphicImage name="#{logoHandler.getLogo('A1')}"/>
Upvotes: 5