String Concatenation in EL [duplicate]
Asked Answered
S

7

61

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?

Solana answered 3/9, 2010 at 23:35 Comment(0)
S
50

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'}
Swagger answered 4/9, 2010 at 14:21 Comment(0)
I
109

With EL 2 you can do the following:

#{'this'.concat(' is').concat(' a').concat(' test!')}
Impossible answered 11/5, 2012 at 10:32 Comment(7)
${'this'.concat(' is').concat(' a').concat(' test!')} would be the normal form.Zonked
Does this work with non-string types, e.g. ${'I am'.concat(' number ').concat(1).concat('!')}?Critique
@MartinCarney concat() does only support string argumentsImpossible
You need EL 2.2 to call non-getter method (see How can I check what version of EL is server using)Midgut
@Impossible doesn't Java coerce the (non-String) argument of String.concat() to String automatically calling its toString() method?Fancied
@Mindwin I haven't used EL for a while, but with normal Java definitely not. If it's a non-null object, you could call toString() on it though.Impossible
@joel EL coerces the argument before submitting to backing Java code.Fancied
S
50

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'}
Swagger answered 4/9, 2010 at 14:21 Comment(0)
O
48

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}" />
Osman answered 3/9, 2010 at 23:49 Comment(1)
The += method above by far is the most correct solution. Thank you!Adena
C
14

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.
Component answered 7/11, 2013 at 5:58 Comment(0)
S
1

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 }
Settlings answered 5/9, 2014 at 12:18 Comment(0)
Q
0

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 ?

Quincunx answered 4/9, 2010 at 14:28 Comment(2)
indent four spaces for code blocks and use the backtick character to escape angle-brackets. stackoverflow.com/editing-helpSwagger
The exact same solution as BalusC, but a day later? Hmm...Adalineadall
B
0

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

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

Bibliography answered 1/12, 2014 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.