Previous Section  < Day Day Up >  Next Section

B.3 Data Types

A variable is always of a specific Java type. Besides the standard Java types for numeric, Boolean, and text values, the EL provides special support for custom classes developed according to the JavaBeans guidelines, java.util.Map objects, java.util.List objects, and arrays.

Bean properties can be accessed using the special property accessor operator (a dot), and be nested to any length:

#{aBean.aProperty.aPropertyOfTheProperty.andSoOn}

Map entries can be accessed the same way:

#{aMap.aKey}

List and array elements can be accessed using the array accessor operator (square brackets):

#{aList[0]}

#{anArray[0]}

#{anArrayOrList[anExressionWithANumbericValue]}

The array accessor operator can also access bean properties and Map entries. It must be used when the property name is determined by a subexpression, the property name is a reserved word or contains characters used for operators, such as a dot:

#{aMap[param.customerName]}

#{aBean['empty']}

#{aMap['com.mycomp.logo']}

A custom javax.faces.el.PropertyResolver class can extend or modify how the property and element accessor operators behave. See Appendix D for details.

B.3.1 Coercion Rules

The EL automatically converts, or coerces, variable values and the result of an expression to the type required by an operator:

To Java type

Conversion rule

String

null: to empty string ("").

All other types: to the corresponding String value.

Primitive number or Number

null or empty string: 0.

Character or char: to the value represented by the character code.

String: parse as an Integer or Floating point literal.

Numeric types: coerce to the requested precision.

boolean or Boolean

null: to false.

String: to true if the value is "true", ignoring case, otherwise false.

Other type

null: keep as null.

String: use the PropertyEditor for the requested type, if any, otherwise null if the string is empty.

Other: type cast, if possible.

In all cases, the EL evaluator throws an exception for attempts to convert between types not defined in the table or if the defined conversion rule fails.

    Previous Section  < Day Day Up >  Next Section