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

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

问题描述

如何使用 c 标签验证 String 是否为空或为空> JSTL ?

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

我想验证它为null还是空(我的值是字符串)。

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

推荐答案


如何使用JSTL的c标记验证字符串为空还是空? / em>

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

您可以在empty 关键字//docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/if.html rel = noreferrer> < c:if> 为此:

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

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

要了解有关 $ {} 的更多信息(一种href = https://stackoverflow.com/tags/el/info>表达语言,它与 JSTL ),在此处检查

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

  • How does EL empty operator work in JSF?

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

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