JSP EL String concatenation
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"))}
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.