views:

615

answers:

3

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?

+2  A: 

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}".

romaintaz
Your second example could also be rendered="#{! empty myBean.list}", since 'empty' is a standard JSF EL operator. I prefer to use the first method because it keeps the EL simpler if you ever have to add more conditions.
Colin Gislason
backend method plus "rendered" attr worked ok for me,thanks
sergionni
+1  A: 

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

Bozho
+1  A: 

Not a direct answer, but just a global recommendation: from JSTL, only the functions taglib is of great value in JSF. For the remnant of JSTL, JSF already supports most of the functionality of the JSTL core and format taglibs out of the box (assuming that you're using Facelets as view technology; with JSP you'll still need to grab for example Tomahawk to have a JSF replacement for c:forEach). For example the aforementioned rendered attribute instead of the JSTL c:if and c:choose, the h:outputLink instead of c:url, the f:setPropertyActionListener and f:param instead of c:set and c:param, the ui:repeat instead of c:forEach. Further on also the f:loadBundle and consorts instead of whole fmt taglib. The JSTL sql and xml taglibs are not worth the effort in real work, so they're not worth to use in JSF as well.

The major problem with mixing JSTL+JSF is that they doesn't run "in sync" as you'd intuitively expect from the coding. Roughly said, it's JSTL which processes the page from top to bottom first and then hands over the entire result to JSF which does its task from top to bottom again. This may lead to unexpected results and scoping problems, especially with JSTL tags inside iterated JSF components such as UIData. Keep this in mind!

BalusC