O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conditionally Disable Components Using Bean Properties

A better approach to conditionally include and disable components is to externalize all details for these decisions to a Java class. Example 6-6 shows this approach.

Example 6-6. Menu area using value expressions for conditional rendering (expense/stage1/menuArea.jsp)
<%@ page contentType="text/html" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:view>
  <h:form>
    <table cellpadding="0" cellspacing="0" width="100%">
      <tr>
        <td>
          <h:commandButton value="New" 
            disabled="#{reportHandler.newDisabled}" />
          <h:commandButton value="Delete" 
            disabled="#{reportHandler.deleteDisabled}" />
          <h:commandButton value="Submit" 
            disabled="#{reportHandler.submitDisabled}" />
          <h:commandButton value="Accept" 
            rendered="#{reportHandler.acceptRendered}"
                  disabled="#{reportHandler.acceptDisabled}" />
          <h:commandButton value="Reject" 
            rendered="#{reportHandler.rejectRendered}"
                              disabled="#{reportHandler.rejectDisabled}" />
        </td>
        <td align="right">
          You're logged in as "${pageContext.request.remoteUser}"
          [<h:outputLink value="../../logout.jsp" />
             <h:outputText value="Logout" />
           </h:outputLink>]
          [<h:outputLink value="prefUser.faces" />
             <h:outputText value="Preferences" />
           </h:outputLink>]
        </td>
      </tr>
    </table>
  </h:form>
</f:view>

All HTML form elements have an attribute named disabled. When set to true, the browser disables the corresponding user interface widget: a button can’t be clicked, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required