Given a scenario, write EL code that uses the following operators: property access (the '.' operator), collection access (the '[]' operator).

The EL borrows the JavaScript syntax for accessing structured data as either a property of an object (with the '.' operator) or as a named array element (with the ["name"] operator). JavaBeans component properties and java.util.Map entries, using the key as the property name, can be accessed this way. Here are some examples:

As shown here, an EL expression must always be enclosed within '${' and '}' characters. The first three expressions access a property named myProperty in an object represented by a variable named myObj. The fourth expression access a property with a name that's held by a variable. Instead of a single variable, this syntax can be used with any expression that evaluates to the property name.

The ARRAY ACCESS operator is also used for data represented as a collection of indexed elements, such as a Java array or a java.util.List:

${myList[aVar + 1]}

Expressions with syntax '${identifier[subexpression]}' are evaluated as follows:

  1. Evaluate the identifier and the subexpression; if either resolves to null, the expression is null.

  2. If the identifier is a BEAN:

    The subexpression is coerced to a String value and that string is regarded as a name of one of the bean's properties. The expression resolves to the value of that property; for example, the expression ${name.["lastName"]} translates into the value returned by name.getLastName().

  3. If the identifier is an ARRAY:

    The subexpression is coerced to an int value and the expression resolves to identifier[subexpression]. For example, for an array named colors, colors[3] represents the fourth object in the array. Because the subexpression is coerced to an int, you can also access that color like this: colors["3"]; in that case, JSTL coerces "3" into 3.

  4. If the identifier is a LIST:

    The subexpression is also coerced to an int and the expression resolves to the value returned from identifier.get(subexpression), for example: colorList[3] and colorList["3"] both resolve to the fourth element in the list.

  5. If the identifier is a MAP:

    The subexpression is regarded as one of the map's keys. That expression is not coerced to a value because map keys can be any type of object. The expression evaluates to identifier.get(subexpression), for example, colorMap[Red] and colorMap["Red"]. The former expression is valid only if a scoped variable named Red exists in one of the four JSP scopes and was specified as a key for the map named colorMap.

Table 7.1. Summary of [] collection access operator

Identifier typeExample useMethod invoked







Array.get(colorArray, 2)








You access a map's values through its keys, which you can specify with the [] operator, for example, in table above, ${colorMap[red]} and ${colorMap["red"]}. The former specifies an IDENTIFIER for the key, whereas the latter specifies a STRING. For the identifier, the PageContext.findAttribute method searches all FOUR JSP scopes (searching the page, request, session, and application scopes) for a scoped variable with the name that you specify, in this case, red. On the other hand, if you specify a string, it's passed directly to the map's get method.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide