评估空或空的 JSTL c 标签 [英] Evaluate empty or null JSTL c tags

查看:24
本文介绍了评估空或空的 JSTL c 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JSTLc 标签验证String 是否为空或为空?

How can I validate if a String is null or empty using the c tags of JSTL?

我有一个名为 var1 的变量,我可以显示它,但我想添加一个比较器来验证它.

I have a variable of name var1 and I can display it, but I want to add a comparator to validate it.

<c:out value="${var1}" />

我想验证它何时为空或为空(我的值为字符串).

I want to validate when it is null or empty (my values are strings).

推荐答案

如何使用 JSTL 的 c 标签验证字符串是否为 null 或为空?

您可以在 为此:

You can use the empty keyword in a <c:if> for this:

<c:if test="${empty var1}">
    var1 is empty or null.
</c:if>
<c:if test="${not empty var1}">
    var1 is NOT empty or null.
</c:if>

或者<c:选择>:

<c:choose>
    <c:when test="${empty var1}">
        var1 is empty or null.
    </c:when>
    <c:otherwise>
        var1 is NOT empty or null.
    </c:otherwise>
</c:choose>

或者,如果您不需要有条件地渲染一堆标签,因此您只能在标签属性中检查它,那么您可以使用 EL 条件运算符 ${condition?valueIfTrue : valueIfFalse}:

Or if you don't need to conditionally render a bunch of tags and thus you could only check it inside a tag attribute, then you can use the EL conditional operator ${condition? valueIfTrue : valueIfFalse}:

<c:out value="${empty var1 ? 'var1 is empty or null' : 'var1 is NOT empty or null'}" />

要了解有关那些 ${} 的更多信息(表达式语言,它是来自 JSTL 的单独主题),点击此处.

To learn more about those ${} things (the Expression Language, which is a separate subject from JSTL), check here.

这篇关于评估空或空的 JSTL c 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆