Java Reference
In-Depth Information
Both . and [] operator can be used with JavaBeans or Maps. For example, Listing 3-18 could be
written using the . operator as shown here:
${books.Groovy}
If there is neither a String literal nor an index with or without quotes inside the brackets of the [ ]
operator and if the content inside the brackets of the [ ] operator is not an EL implicit object, then the
content is evaluated by searching for an attribute with that name in any of the four scopes. This is
illustrated using Listing 3-19 and Listing 3-20.
Listing 3-19. Code Fragment for Setting a Map as an Attribute in the Servlet Code
1.Map<String, String> bookMap = new HashMap<>();
2.bookMap.put("Groovy", "Beginning Groovy");
3.bookMap.put("Java", " Beginning Java");
4.bookMap.put("Scala", " Beginning Scala");
5.request.setAttribute("books", bookMap);
6.request.setAttribute("java", "Java");
Listing 3-20. Using the [ ] Operator
Book : ${books[java] }
Let's see how the evaluation works in Listing 3-20.
${ books[java] } of Listing 3-20, EL searches for an attribute bound
by the name books in the scopes.
EL finds this attribute in the request scope because
In the code
books was set as a request
attribute on line 5 in Listing 3-19.
java , which is neither a String
literal nor an EL implicit object; therefore, EL searches for an attribute bound by
the name java in the scopes and finds it in the request scope because java was
set as a request attribute on line 6 in Listing 3-19.
In Listing 3-20, the content of the [ ] operator is
Java of java on line 6 of Listing 3-19, the EL expression now
becomes ${ books["Java"] } .
Now, because the
Using the value
books attribute is a Map set as a request attribute on line 5 in
Listing 3-19, EL searches for the key Java , which is on line 3 in Listing 3-19. and
prints its value as shown in the following output:
Book : Beginning Java
If there is an EL expression inside the brackets of the [] operator, the content inside the brackets,
which is an EL expression in this case, is evaluated by the same rules applied to any EL expression.
In other words, if the EL expression is using the dot operator or [] operator and if it is a [] operator,
then the same rules as explained previously are applied. This is illustrated using Listing 3-21 and
Listing 3-22.
Search WWH ::




Custom Search