4

Can i do conditional logic with JSF, without JSTL tags ?

For example, i made a composite component, and want to state, if the 'id' attribute is specified, then define the 'id' attribute, but if the 'id' attribute is not specified, then, dont specify the 'id' attribute.

For now i have to use rendered attribute, but so many duplications in there, with only slight difference in specifying id or not.

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

Any ideas to avoid this duplication and do the conditional stuff more easily ?

Thank you !

Bertie
  • 17,277
  • 45
  • 129
  • 182

1 Answers1

2

Specify a default id yourself.

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

It will be unique anyway since the componsite component's own client ID will be prepended.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555