Concatenation of property name (EL) in JSF

会有一股神秘感。 提交于 2019-12-12 02:15:51

问题


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".


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/6249086/concatenation-of-property-name-el-in-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!