Section 5.4. The JSF Expression Language


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.

Table 5-1. Built-in JSF Expression Language objects

EL object

Definition

applicationScope

A Map containing all application scope-managed beans

cookie

A Map of the HTTP cookies for the current request, by name

facesContext

The FacesContext instance associated with the current request

header

A Map with all HTTP headers submitted with the current request, containing one value for each header

headerValues

A Map with all HTTP headers submitted with the current request, containing a String[] array for each header with all submitted values

initParam

A Map containing each of the initialization parameters for the FacesServlet

param

A Map with all HTTP parameters submitted with the current request, containing one value for each parameter

paramValues

A Map with all HTTP parameters submitted with the current request, containing a String[] array for each parameter with all submitted values

requestScope

A Map containing all request scope-managed beans

sessionScope

A Map containing all session scope-managed beans

view

The UIViewRoot object associated with the current request


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

Table 5-2. JSF Expression Language operators

Operator type

Operators

  

Arithmetic

+

-

/

%

  
  

div

mod

  

Logical

&&

||

!

   

and

 

not

   

Relational

<

<=

>

>=

==

!=

lt

le

gt

ge

eq

ne

Selection

?

     


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.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net