geeehhdaa
geeehhdaa

Reputation: 822

Concatenation of property name (EL) in JSF

How can I concatenate the name of a property using the EL?

This is what I tried:

<ui:repeat value="#{someBean.getParts()}" var="part">  
        <h:inputTextarea value="#{someOtherBean.result}#{part}" />      
</ui:repeat>  

But it didn't work.

The bean has the four property resultA, resultB, resultC and resultD. getParts() returns "A", "B", "C", and "D".

Upvotes: 0

Views: 3871

Answers (2)

BalusC
BalusC

Reputation: 1108632

It's quite possible though. You can use <ui:param> to prepare the dynamic property name and use the brace notation [] to access it.

<ui:repeat value="#{someBean.parts}" var="part">  
    <ui:param name="resultPart" value="result#{part}" />
    <h:inputTextarea value="#{someOtherBean[resultPart]}" />
</ui:repeat>

Needless to say that I agree with Michael that this is a smell in the model design.

Upvotes: 4

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

I don't think that can be made to work without changing the design. It's generally a bad idea in Java to have a design that requires you to access methods fields and properties through a name, and worse if the name is built from strings.

Possible solutions:

  • have getParts() return "resultA", "resultB", etc. and access them #{someOtherBean[getParts()]}
  • change the property names to a, b, c, d and access them as #{someOtherBean[getParts()]}
  • have a single property result that contains a Map with "A", "B", etc as keys and access the values as #{someOtherBean.result[getParts()]}

Upvotes: 3

Related Questions