<c:if> tag for JSF page
-
10-07-2019 - |
Question
Used to develop Portlets and JPS's, so have a dilemma.
task: display <h:dataTable>
only when list isn't empty
Is it possible to provide <c:if>
tag for JSF page?
Solution
Yes, you can add JSTL tags in your JSF pages. However, in your case, you can simply do the following:
<h:dataTable value="#{myBean.list}"... rendered="#{myBean.displayTable}">
...
</h:dataTable>
with the following method in myBean
:
public boolean isDisplayTable() {
return list != null && list.size() > 0;
}
You can also try that (not sure if it will work):
<!-- Idea from Colin Gislason -->
<h:dataTable value="#{myBean.list}"... rendered="#{not empty myBean.list}">
...
</h:dataTable>
<h:dataTable value="#{myBean.list}"... rendered="#{not myBean.list.empty}">
...
</h:dataTable>
Note that you can use the !
instead of the not
keyword in the EL expressions (see a guide here about EL expressions), which results in rendered="#{!empty myBean.list}"
.
OTHER TIPS
<c:if>
(and other JSTL tags) can be used in JSF in most cases. However, the cannot be used inside components that iterate on collections - like <h:dataTable>
. But in your case, you want the <c:if>
outside, so it is possible. However, as romaintaz pointed, it's better to use the rendered
attribute.