如何在 EL 中连接字符串?

发布于 2024-11-14 18:15:04 字数 225 浏览 3 评论 0原文

如何获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

羁〃客ぐ 2024-11-21 18:15:04

如果您已经使用 EL 3.0(Java EE 7;WildFly、Tomcat 8+、GlassFish 4+、Payara、WAS 9+ 等),那么您可以使用新的 += 运算符来实现此目的:

<p>${not empty promoPrice ? 'ONLY 

如果您还没有使用 EL 3.0,请使用 EL 2.2(Java EE 6;JBoss AS 6/7、Tomcat 7、GlassFish 3、WAS 8 等)使用参数调用直接方法的能力,然后将其应用于 String#concat()

<p>${not empty promoPrice ? 'ONLY 

或者如果您还没有使用 EL 2.2(Java EE 5 或更早版本;JBoss AS 5- Tomcat 6-、WAS 7- 等),然后使用 JSTL 创建一个新的 EL 变量,其中串联的值刚刚内联到 value 中:

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

您的特定情况中,另一种方法是将表达式分成两部分:

<p>${not empty promoPrice ? 'ONLY 

如果${promoPrice}为null或空,则无论如何都不会打印它。

+= promoPrice : 'FREE'}</p>

如果您还没有使用 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或空,则无论如何都不会打印它。

.concat(promoPrice) : 'FREE'}</p>

或者如果您还没有使用 EL 2.2(Java EE 5 或更早版本;JBoss AS 5- Tomcat 6-、WAS 7- 等),然后使用 JSTL 创建一个新的 EL 变量,其中串联的值刚刚内联到 value 中:


您的特定情况中,另一种方法是将表达式分成两部分:


如果${promoPrice}为null或空,则无论如何都不会打印它。

+= promoPrice : 'FREE'}</p>

如果您还没有使用 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或空,则无论如何都不会打印它。

: 'FREE'}${promoPrice}</p>

如果${promoPrice}为null或空,则无论如何都不会打印它。

+= promoPrice : 'FREE'}</p>

如果您还没有使用 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或空,则无论如何都不会打印它。

.concat(promoPrice) : 'FREE'}</p>

或者如果您还没有使用 EL 2.2(Java EE 5 或更早版本;JBoss AS 5- Tomcat 6-、WAS 7- 等),然后使用 JSTL 创建一个新的 EL 变量,其中串联的值刚刚内联到 value 中:

您的特定情况中,另一种方法是将表达式分成两部分:

如果${promoPrice}为null或空,则无论如何都不会打印它。

+= promoPrice : 'FREE'}</p>

如果您还没有使用 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:

<p>${not empty promoPrice ? 'ONLY 

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 

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 

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

+= 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():


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.

.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:


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.

+= 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():

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.

: 'FREE'}${promoPrice}</p>

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

+= 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():

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.

.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:

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.

+= 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():

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.

孤君无依 2024-11-21 18:15:04

直接jstl方式

<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>

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>
甜妞爱困 2024-11-21 18:15:04

与 EL 2.0+ 兼容的字符串连接的一个简单而强大的解决方案是使用中间变量:

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

根据 @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:

<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.

捎一片雪花 2024-11-21 18:15:04

我做了类似的事情,我有一个变量 mathjaxUrl 并且我想联系它其他字符串

<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" />

希望这对你有帮助

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

别念他 2024-11-21 18:15:04

这行不通吗?

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

请注意,${promoPrice} 位于引号之外。这看起来是最简单的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文