Conditionally Render Components

So far, the components we’ve created are always shown to the user, but sometimes that’s not what we want. The menu area in the sample application, for instance, contains buttons for accepting or rejecting a report only if the current user is a manager. Component properties may also depend on runtime conditions, e.g., the way the menu area buttons are enabled or disabled depending on the current report’s status in the sample application.

Figure 6-6 shows the menu area as it appears to a regular user for a brand new report without entries, i.e., without the buttons available only to managers and with all buttons disabled.

Menu area
Figure 6-6. Menu area

If you come from a JSP background and have used JSTL a bit, your first impulse is likely to use the JSTL <c:if> or <c:choose> actions for conditional processing along these lines:

<%@ page contentType="text/html" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <f:view> <h:form> <table cellpadding="0" cellspacing="0" width="100%"> <tr> <td> <h:commandButton value="New" disabled="#{reportHandler.currReport.status == 0}" /> <h:commandButton value="Delete" disabled="#{reportHandler.currReport.status == 0 || reportHandler.currReport.status == 2 || reportHandler.currReport.status == ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.