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:

mapName.THIS_KEY

mapName.ANOTHER_KEY

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."

REF: http://www.ibm.com/developerworks/java/library/j-jstl0211/index.html
I can see why, because it allows multi-dimensional maps to be
referenced like this:

mapName.ANOTHER_KEY.SOMEKEY_INSIDE_THAT_INSIDE_MAP

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: