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>
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>
Straight jstl way
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:If you're however not on EL 3.0 yet, 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()
: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:In your particular case, another way is to split the expression in two parts:
If
${promoPrice}
is null or empty, it won't be printed anyway.A straightforward and robust solution for string concatenation, that is compatible with EL 2.0+, is to use an intermediate variable:
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.Won't this work ?
Notice that the ${promoPrice} is outside the quotes. This looks like the simplest solution.
I did something like this where I have a variable
mathjaxUrl
and I want to contact it other stringhope this help you