user2768087
user2768087

Reputation: 23

JSF dynamic resource path

How can I use the resource variable to create a dynamic path?

<ui:repeat value="#{backgroundImageBean.images}" var="image"> 
      <p:graphicImage value="#{resource['/path/#{image}']}" 
       style="width: 100%; height: 100%" /> 
</ui:repeat>

Thanks

Upvotes: 2

Views: 975

Answers (2)

BalusC
BalusC

Reputation: 1109655

Just use name attribute instead of value attribute. The value attribute takes an URL while the name attribute takes the sole resource name already. It's then under the covers resolved the same way as #{resource[name]}.

<p:graphicImage name="path/#{image}" />

Upvotes: 4

skuntsel
skuntsel

Reputation: 11742

Your problem can be solved in one of the two ways:

  1. Embed the '/path/' part into your model so that #{image} would return the full path to the resource;
  2. Create an alias for the to-be-generated image path using <ui:param> and use it when accessing the resource:

    <ui:repeat value="#{backgroundImageBean.images}" var="image">
        <ui:param name="path" value="/path/#{image}" />
        <p:graphicImage value="#{resource[path]}" /> 
    </ui:repeat>
    

If you're insisting on doing the logic without using a parameter and your environment supports EL 2.2+ then you can use String#concat() in your resource expression:

#{resource['/path/'.concat(not empty image ? image : '')}'

Upvotes: 2

Related Questions