The JSF Expression Language

Now that we’ve established the existence of managed beans, we can talk about the JSF EL . The EL, which is similar but not identical to the JSP EL described in Chapter 4, allows you to write expressions that reference the managed beans in your application. These expressions serve two main purposes in JSF: retrieving values from beans and telling JSF components which bean properties to update. In some cases, the same expression does both, depending on where you are in the request lifecycle.

The general syntax of an EL expression is #{ identifiers and modifiers }. The #{marks the beginning of the expression, and the } marks the end.

Here’s an example of a simple tag that displays a value from a managed bean. In this case, we’re displaying the username property, which is itself a property of the currentUser property of the usersession bean.

    <h:outputText value="#{usersession.currentUser.username}"/>

The <h:outputText> tag is a built-in JSF tag, used to display the value of an expression. We’ll look at more JSF tags in the next section. When working with input controls, the application uses the value expression first to prepopulate the control and then to store the results of the request. The following tag creates a text input control, associated with the username property of the loginform bean:

    <h:inputText value="#{loginform.username}"/>

When the user first loads the JSF page containing this tag, the application displays an HTML input field containing the current ...

Get Java Enterprise in a Nutshell, Third Edition 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.