如何在 EL 中连接字符串?
如何获取 promoPrice
变量作为字符串 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您已经使用 EL 3.0(Java EE 7;WildFly、Tomcat 8+、GlassFish 4+、Payara、WAS 9+ 等),那么您可以使用新的
+=
运算符来实现此目的:如果您还没有使用 EL 3.0,请使用 EL 2.2(Java EE 6;JBoss AS 6/7、Tomcat 7、GlassFish 3、WAS 8 等)使用参数调用直接方法的能力,然后将其应用于
String#concat()
:或者如果您还没有使用 EL 2.2(Java EE 5 或更早版本;JBoss AS 5- Tomcat 6-、WAS 7- 等),然后使用 JSTL
创建一个新的 EL 变量,其中串联的值刚刚内联到 value 中:在您的特定情况中,另一种方法是将表达式分成两部分:
如果
: 'FREE'}${promoPrice}</p>${promoPrice}
为null或空,则无论如何都不会打印它。如果
+= promoPrice : 'FREE'}</p>${promoPrice}
为null或空,则无论如何都不会打印它。如果您还没有使用 EL 3.0,请使用 EL 2.2(Java EE 6;JBoss AS 6/7、Tomcat 7、GlassFish 3、WAS 8 等)使用参数调用直接方法的能力,然后将其应用于
String#concat()
:或者如果您还没有使用 EL 2.2(Java EE 5 或更早版本;JBoss AS 5- Tomcat 6-、WAS 7- 等),然后使用 JSTL
创建一个新的 EL 变量,其中串联的值刚刚内联到 value 中:在您的特定情况中,另一种方法是将表达式分成两部分:
如果
.concat(promoPrice) : 'FREE'}</p>${promoPrice}
为null或空,则无论如何都不会打印它。或者如果您还没有使用 EL 2.2(Java EE 5 或更早版本;JBoss AS 5- Tomcat 6-、WAS 7- 等),然后使用 JSTL
创建一个新的 EL 变量,其中串联的值刚刚内联到 value 中:在您的特定情况中,另一种方法是将表达式分成两部分:
如果
+= promoPrice : 'FREE'}</p>${promoPrice}
为null或空,则无论如何都不会打印它。如果您还没有使用 EL 3.0,请使用 EL 2.2(Java EE 6;JBoss AS 6/7、Tomcat 7、GlassFish 3、WAS 8 等)使用参数调用直接方法的能力,然后将其应用于
String#concat()
:或者如果您还没有使用 EL 2.2(Java EE 5 或更早版本;JBoss AS 5- Tomcat 6-、WAS 7- 等),然后使用 JSTL
创建一个新的 EL 变量,其中串联的值刚刚内联到 value 中:在您的特定情况中,另一种方法是将表达式分成两部分:
如果
${promoPrice}
为null或空,则无论如何都不会打印它。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: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()
: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:In your particular case, another way is to split the expression in two parts:
If
: 'FREE'}${promoPrice}</p>${promoPrice}
is null or empty, it won't be printed anyway.If
+= promoPrice : 'FREE'}</p>${promoPrice}
is null or empty, it won't be printed anyway.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()
: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:In your particular case, another way is to split the expression in two parts:
If
.concat(promoPrice) : 'FREE'}</p>${promoPrice}
is null or empty, it won't be printed anyway.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:In your particular case, another way is to split the expression in two parts:
If
+= promoPrice : 'FREE'}</p>${promoPrice}
is null or empty, it won't be printed anyway.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()
: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: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.直接jstl方式
Straight jstl way
与 EL 2.0+ 兼容的字符串连接的一个简单而强大的解决方案是使用中间变量:
根据 @BalusC,从 EL 2.2 开始,您可以使用
String#concat()
方法进行串联,从 EL 3.0 开始,您可以使用新的+=
运算符。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.我做了类似的事情,我有一个变量
mathjaxUrl
并且我想联系它其他字符串希望这对你有帮助
I did something like this where I have a variable
mathjaxUrl
and I want to contact it other stringhope this help you
这行不通吗?
请注意,${promoPrice} 位于引号之外。这看起来是最简单的解决方案。
Won't this work ?
Notice that the ${promoPrice} is outside the quotes. This looks like the simplest solution.