JSP

Nested Expression Language quirk

Tuesday, October 28th, 2008 | Java, JSP, Tech-savvy | No Comments

Today I stumbled over a rather unusual EL example. I had to access a value of a HashMap on the PageContext. The key to the value was composed of two other (EL) values.

Imagine some x containing some y, the key we are looking for is the compound of the current x id and the current y id, to look up a value in another place. Dazed and confused? 😉

This looks odd but is almost the solution:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 
  ${hashmap[${x.id}${y.id}]}

Hmm, nested EL looks evil and *is* evil, it will give you an ELException: javax.servlet.jsp.el.ELException: Encountered “{“, expected one of [“.”, “>”, “gt”, “<“, “lt”, “==”, “eq”, “<=”, “le”, “>=”, “ge”, “!=”, “ne”, “[“, “]”, “+”, “-“, “*”, “/”, “div”, “%”, “mod”, “and”, “&&”, “or”, “||”, “:”, “(“, “?”]

The solution is substitution. Btw, I hated that in maths (square root from negative numbers, yeah right… 🙄 ).

The following code substitutes the nested EL resulting in a correctly running JSP.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 
  ${x.id}${y.id}
  ${hashmap[xandy]}

There may be other solutions for this problem, feel free to enlighten me and leave a comment.

Tags: , , ,

Search

Categories