JSP EL String concatenation

Kyle picture Kyle · Jul 6, 2010 · Viewed 92.4k times · Source

How do I concatenate strings in EL?

I want to do something like this but it doesn't work:

${var1 == 0 ? 'hi' : 'hello ' + var2}

It throws an exception trying to cast 'hello' to a Double

Answer

Mike Haefele picture Mike Haefele · Mar 28, 2011

Using java string concatenation works better.

#{var1 == 0 ? 'hi' : 'hello'.concat(var2)}

The benefit here is you can also pass this into a function, for instance

#{myCode:assertFalse(myVar == "foo", "bad myVar value: ".concat(myVar).concat(", should be foo"))}