Thursday, 29 November 2012

JSTL - map contents can be referenced using (.) dot operator instead of brackets.

I was reviewing some code and it had these map references but they
were of the format:



Then I googled and saw that:

"Finally, the dot operator and the bracket operator are somewhat
interchangeable. For example, ${user["firstName"]} could also be used
to retrieve the firstName property of the user object, just as
${commands.dir} could be used to fetch the value associated with the
"dir" key in the commands map."

I can see why, because it allows multi-dimensional maps to be
referenced like this:


and won't go Boom! when the ANOTHER_KEY lookup returns nothing.

I still find it kinda icky. I think the dot expression should really
be used sparingly.

If you're doing lookups only one level deep, then it is so much easier
to understand when you do map lookups the same way you do it in normal
Java code.

No comments: