JSP EL String concatenation [duplicate]
Asked Answered
K

4

82

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

Kicker answered 6/7, 2010 at 19:47 Comment(2)
#296898Marconigraph
Just use 'hello'.concat(var2)Surat
F
70

The + operator always means numerical addition in JSP Expression Language. To do string concatenation you would have to use multiple adjacent EL expressions like ${str1}${str2}.

If I read your example correctly this could be written as:

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

Edit

Another possibility would be to use JSTL, which is longer but might be clearer if there is more text that depends on var1:

<c:choose>
    <c:when test="${var1 == 0}">hi</c:when>
    <c:otherwise>hello <c:out value="${var2}"/></c:otherwise>
</c:choose>

The c:out might not be needed, depending on the JSP version.

Funereal answered 6/7, 2010 at 20:4 Comment(1)
I was hoping I wouldn't have to do that... repeating the condition every time. My example is a simplification, in the real scenario I would have to repeat the example 3 times each time i want to do it :(Kicker
E
85

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"))}
Eusebioeusebius answered 28/3, 2011 at 15:45 Comment(1)
Please note that this only works in the fresh new EL 2.2 and not in the older EL versions between 2001 and now.Comptometer
F
70

The + operator always means numerical addition in JSP Expression Language. To do string concatenation you would have to use multiple adjacent EL expressions like ${str1}${str2}.

If I read your example correctly this could be written as:

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

Edit

Another possibility would be to use JSTL, which is longer but might be clearer if there is more text that depends on var1:

<c:choose>
    <c:when test="${var1 == 0}">hi</c:when>
    <c:otherwise>hello <c:out value="${var2}"/></c:otherwise>
</c:choose>

The c:out might not be needed, depending on the JSP version.

Funereal answered 6/7, 2010 at 20:4 Comment(1)
I was hoping I wouldn't have to do that... repeating the condition every time. My example is a simplification, in the real scenario I would have to repeat the example 3 times each time i want to do it :(Kicker
W
45

I know this is an old topic, but the answer to this question has changed in the past six months, and it's important to note that change, IMO (since I found this by Googling "el concatenate strings").

As of EL Expression 3.0 (public ballot approved August 2012, released with Java EE 7), a twist on the syntax the questioner originally used is now valid:

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

There was much disagreement with the use of += instead of +, but it is what it is. This will correctly evaluate and concatenate the strings as expected. You can also use the cat operator instead of the += operator:

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

While this is now legal, note that you won't be able to use it until your web container (Tomcat, Jetty, GlassFish, etc.) releases a new version that supports Java EE 7/EL 3.0. This is expected sometime before the end of 2013, perhaps as early as the fall.

Edited 2015-02-19 to note that the final operator was += and not + as originally answered.

Wherein answered 15/2, 2013 at 4:52 Comment(4)
Great answer to see future! +1Outsail
Unfortunately this is not correct. At the last moment to much grievance of pretty much everyone they changed the operator from the completely logical "+" to the highly confusing "+=". I don't know what these guys were smoking :XLiebig
Dexter is correct. Thanks for the clarification. I will edit my answer.Wherein
Dear Nick Williams - how to concatenation work on your example: Full Name: ${fn:escapeXml(user.lastName) += ', ' += fn:escapeXml(user.firstName)} . This bit of code is from your book(chap6 profile.jsp). I don't know - but error occures.Geotropism
M
14

One more alternative to all that was already mentioned:

<c:set var="hellovar2" value="hello ${var2}" />
${var1 == 0 ? 'hi' : hellovar2}
Mispickel answered 18/10, 2012 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.