I'm programming a piece of code that should display some html if a statement is correct.
I have a managed-bean which contains the values. The status, is an enum - and I've found online that it could be treated as a string. However, it does not work for me.
<c:if test="#{myBean.item.status == 'OPEN'}" >
//display this
</c:if>
What is the best way to display this? The c:if
doesn't work.
The <c:if>
runs during view build time, that moment when the XHTML file is converted to a JSF component tree, and would not work if the to be tested variable is only available during view render time, that moment when the JSF component tree needs to generate HTML. You would then need a JSF component which runs during view render time.
If you intend to render JSF component fragments conditionally during view render time, then you should be using <ui:fragment>
:
<ui:fragment rendered="#{myBean.item.status == 'OPEN'}">
...
</ui:fragment>
An alternative is <h:panelGroup>
:
<h:panelGroup rendered="#{myBean.item.status == 'OPEN'}">
...
</h:panelGroup>
It renders also nothing to the HTML output as long as you don't specify any attribtues which should end up in the HTML output like id
, style
, etc. It would then generate a HTML <span>
element. This is semantically at least much better than a <label>
which is incorrectly suggested by the other answerer. A <label>
is intented to label an associated HTML input element, not to span some markup.