Java Enterprise in a Nutshell (In a Nutshell (OReilly))
5.4. 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 value of the EL expression. In this case, the field will be blank since the login form bean is request scoped. When the form is submitted, the JSF framework will use the same EL expression to decide where to store the form values the browser sends: in this case, into the username property of the loginform bean. The EL also supports lists and maps, allowing you to access more dynamic information. Many of the built-in EL objects (listed in Table 5-1) rely on this functionality. When referencing a Map, the identifier on the righthand side of the period is used as the key for retrieving a value from the map. For example, using the built-in attribute sessionScope, we can retrieve the usersession bean like this: #{sessionScope.usersession.currentUser.username}
An equivalent expression would be: #{sessionScope['usersession'].currentUser.username}
This is an alternative syntax for retrieving values from a map. The square-bracket syntax is generally used when retrieving values with (.) characters in their keys, which is often necessary when dealing with properties files and message bundles for internationalization.
You can also perform calculations with EL expressions. The EL doesn't allow you to write value assignment statements directly, which frees page developers from the temptation to write too much business logic into the JSP files themselves. But as we'll see, many tags take Boolean parameters and it's often helpful to be able to express them in the EL rather than writing lots of extra methods in your beans. The alphabetic versions of the relational operators, which are listed below the regular versions in Table 5-2, exist to avoid conflict with JSP, XML, and HTML tags (having too many angle brackets running around gets confusing for the parsers!).
The ? selection operator works the same way it does in Java. For example, we can use it for some simple formatting: <h:outputText value="#{bookdisplayform.book.checkedOut ? 'Out' : 'In'}"/> The selection operator is often useful for controlling component rendering on JSF pages when a bean method does not return true or false. |