开发者

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. get开发者_高级运维Parts() returns "A", "B", "C", and "D".


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.


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()]}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜