How to concatenate a String in EL?
Asked Answered
K

5

26

How do I get the promoPrice variable to print as part of the string ONLY $4.67?

<c:set var="promoPrice" value="4.67" />
<p>${(promoPrice != null) ? "ONLY $${promoPrice}" : "FREE"}</p>
Kato answered 9/6, 2011 at 18:7 Comment(2)
did my suggestion solve your problem? i.e. move the $promoPrice outside the quotes.Stoltz
@rationalSpring no, that doesn't work. I guess you can't pass the variable that way. Thanks though.Kato
B
45

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

<p>${not empty promoPrice ? 'ONLY $' += promoPrice : 'FREE'}</p>

If you're however not on EL 3.0 yet, then use EL 2.2 (Java EE 6; JBoss AS 6/7, Tomcat 7, GlassFish 3, WAS 8, etc) capability of invoking direct methods with arguments, which you then apply on String#concat():

<p>${not empty promoPrice ? 'ONLY $'.concat(promoPrice) : 'FREE'}</p>

Or if you're even not on EL 2.2 yet (Java EE 5 or older; JBoss AS 5- Tomcat 6-, WAS 7-, etc), then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:

<c:set var="promoPriceString" value="ONLY $${promoPrice}" />
<p>${not empty promoPrice ? promoPriceString : 'FREE'}</p>

In your particular case, another way is to split the expression in two parts:

<p>${not empty promoPrice ? 'ONLY $' : 'FREE'}${promoPrice}</p>

If ${promoPrice} is null or empty, it won't be printed anyway.

Brummett answered 9/6, 2011 at 18:10 Comment(2)
thank you very much! For some reason I couldn't find info anywhere about this kind of concatenation.Kato
'str1'.concat(' str2') fails in EL 2.1 (Tomcat 6) with this error org.apache.jasper.JasperException: /WEB-INF/pages/x.jsp The function concat must be used with a prefix when a default namespace is not specified.Warship
H
3

Straight jstl way

<c:set var="promoPrice" value="4.67" />
<p>
<c:choose>
    <c:when test="${(promoPrice != null)}">
        ONLY $${promoPrice}
    </c:when>
    <c:otherwise>
        FREE
    <c:otherwise>
</c:choose>
</p>
Heterophony answered 9/6, 2011 at 18:11 Comment(2)
I like this version, I was just looking for something a little more compact. Thanks!Kato
yeah, its a bit verbose but its pretty clear especially if you are using jstl other placesHeterophony
W
1

A straightforward and robust solution for string concatenation, that is compatible with EL 2.0+, is to use an intermediate variable:

<c:set var="promoPrice" value="4.67" />
<c:set var="priceText" value="ONLY ${promoPrice}" />
<p>${(promoPrice != null) ? priceText : "FREE"}</p>

According to @BalusC, starting from EL 2.2 you can do concatenation using String#concat() method, and starting from EL 3.0 you can use the new += operator for this.

Warship answered 11/12, 2017 at 11:1 Comment(0)
L
-1

I did something like this where I have a variable mathjaxUrl and I want to contact it other string

<c:set var="mathjaxUrl" value="https://cdnjs.cloudflare.com/ajax/libs/mathjax" />
... some other stuff here
<c:set var="mathjaxUrl" value="${mathjaxUrl}?config=TeX-AMS-MML_HTMLorMML" />

hope this help you

Lofty answered 17/5, 2017 at 10:49 Comment(0)
S
-3

Won't this work ?

<c:set var="promoPrice" value="4.67" />
<p>${(promoPrice != null) ? "ONLY $"${promoPrice} : "FREE"}</p>

Notice that the ${promoPrice} is outside the quotes. This looks like the simplest solution.

Stoltz answered 9/6, 2011 at 18:44 Comment(2)
Why the -1? Could whoever rated this answer explain?Stoltz
this is not legal EL syntax; from the EL specification (2.2 mrel): Nested eval-expressions, such as ${item[${i}]}, are illegal.Daff

© 2022 - 2024 — McMap. All rights reserved.