String Concatenation in EL

JavaStringJspJstlEl

Java Problem Overview


I would like to concatenate a string within a ternary operator in EL(Expression Language).

Suppose there is a variable named value. If it's empty, I want to use some default text. Otherwise, I need to append it with some static text.

${(empty value)? "none" : value + " enabled"}

This will not compile however. What would be a correct way to write this? Or is this even possible?

Java Solutions


Solution 1 - Java

With EL 2 you can do the following:

#{'this'.concat(' is').concat(' a').concat(' test!')}

Solution 2 - Java

This answer is obsolete. Technology has moved on. Unless you're working with legacy systems see Joel's answer.


There is no string concatenation operator in EL. If you don't need the concatenated string to pass into some other operation, just put these expressions next to each other:

${value}${(empty value)? 'none' : ' enabled'}

Solution 3 - Java

If you're already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this:

<c:out value="${empty value ? 'none' : value += ' enabled'}" />

If you're however not on EL 3.0 yet, and the value is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use EL 2.2 (Java EE 7; JBoss AS 6/7, Tomcat 7, GlassFish 3, etc) capability of invoking direct methods with arguments, which you then apply on String#concat():

<c:out value="${empty value ? 'none' : value.concat(' enabled')}" />

Or if you're even not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:

<c:set var="enabled" value="${value} enabled" />
<c:out value="${empty value ? 'none' : enabled}" />

Solution 4 - Java

Since Expression Language 3.0, it is valid to use += operator for string concatenation.

${(empty value)? "none" : value += " enabled"}  // valid as of EL 3.0

Quoting EL 3.0 Specification.

> String Concatenation Operator
> > To evaluate > > A += B > > - Coerce A and B to String. > - Return the concatenated string of A and B.

Solution 5 - Java

Mc Dowell's answer is right. I just want to add an improvement if in case you may need to return the variable's value as:

${ empty variable ? '<variable is empty>' : variable }

Solution 6 - Java

1.The +(operator) has not effect to that in using EL. 2.so this is the way,to use that

<c:set var="enabled" value="${value} enabled" />


<c:out value="${empty value ? 'none' : enabled}" />

is this helpful to You ?

Solution 7 - Java

it also can be a great idea using concat for EL + MAP + JSON problem like in this example :

#{myMap[''.concat(myid)].content}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTom TuckerView Question on Stackoverflow
Solution 1 - JavaJoelView Answer on Stackoverflow
Solution 2 - JavaMcDowellView Answer on Stackoverflow
Solution 3 - JavaBalusCView Answer on Stackoverflow
Solution 4 - JavaAniket KulkarniView Answer on Stackoverflow
Solution 5 - JavaMohammad FaisalView Answer on Stackoverflow
Solution 6 - JavaAdalarasan SachithananthamView Answer on Stackoverflow
Solution 7 - JavaBenjamin FuentesView Answer on Stackoverflow