Using f:selectItems var in passtrough attribute

后端 未结 1 1259
臣服心动
臣服心动 2020-12-11 18:20

can I pass expressions to JSF 2 passthrough-attributes? the following code is not working. expression #{country.isoCode} is not evaluated.



        
相关标签:
1条回答
  • 2020-12-11 18:43

    EL is basically supported/evaluated over all place in a Facelet template. Also outside tags/attributes. Even in HTML comments, where many starters then fall over. So that's not the problem.

    Your particular case is, unfortunately, "by design". Before rendering the first <option> element, the <f:selectItems> is is wholly parsed only once and turned into an iterator during which all EL expressions will be evaluated. Then, the component will iterate over it while rendering <option> elements during which all passthrough attributes will be evaluated. However, as the var was already evaluated during creating the iterator, it isn't available anywhere during rendering the passthrough attributes and ultimately evaluates to an empty string.

    Fixing that would require quite some changes in standard JSF implementation of <f:selectItems>. I'm not sure if JSF guys would be all ears for that, but you can always try to create an issue.

    You can work around this by creating physically multiple <f:selectItem> instances during view build time, with help of <c:forEach>.

    <h:selectOneMenu ...>
        <c:forEach items="#{bean.countries}" var="country">
            <f:selectItem 
                itemValue="#{country}" 
                itemLabel="#{country.countryName}" 
                pt:data-icon="flag flag-#{country.isoCode}" />   
        </c:forEach>             
    </h:selectOneMenu>
    

    See also:

    • JSTL in JSF2 Facelets... makes sense?
    0 讨论(0)
提交回复
热议问题