在JSF中有条件

我可以用JSF做条件逻辑,没有JSTL标签吗?

例如,我创建了一个复合组件,并且想要声明,如果指定了’id’属性,则定义’id’属性,但如果未指定’id’属性,则不要指定’id’属性.

现在我必须使用渲染属性,但那里有很多重复,只是在指定id方面略有不同.

<composite:interface>
    <composite:attribute name="id" />
        ...
</composite:interface>

<composite:implementation>
    <!-- render this when id is specified, id attribute is defined -->
    <h:selectOneMenu 
                id="#{cc.attrs.id}" 
                label="#{cc.attrs.label}"
                value="#{cc.attrs.value}"
                rendered="#{cc.attrs.id != null}" ...>
                ...
    </h:selectOneMenu>

    <!-- render this when id is not specified, id attribute is NOT defined -->
    <h:selectOneMenu 
                label="#{cc.attrs.label}"
                value="#{cc.attrs.value}"
                rendered="#{cc.attrs.id == null}" ...>
                ...
    </h:selectOneMenu>
</composite:implementation>

是否有任何想法可以避免这种重复并更容易地处理有条件的东西?

谢谢 !

最佳答案 自己指定默认ID.

<h:selectOneMenu 
    id="#{not empty cc.attrs.id ? cc.attrs.id : 'menu'}" 
    label="#{cc.attrs.label}"
    value="#{cc.attrs.value}">
    ...
</h:selectOneMenu>

无论如何,它将是唯一的,因为componsite组件自己的客户端ID将被预先添加.

点赞