Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

jsp - 评估空或空JSTL c标记(Evaluate empty or null JSTL c tags)

How can I validate if a String is null or empty using the c tags of JSTL ? (如何使用JSTLc标签验证String是空还是空?)

I have a variable of name var1 and I can display it, but I want to add a comparator to validate it. (我有一个名为var1的变量,我可以显示它,但我想添加一个比较器来验证它。)

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

I want to validate when it is null or empty (my values are strings). (我想验证它是null还是空(我的值是字符串)。)

  ask by user338381 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How can I validate if a String is null or empty using the c tags of JSTL? (如何使用JSTL的c标签验证String是空还是空?)

You can use the empty keyword in a <c:if> for this: (您可以在<c:if>使用empty关键字:)

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

Or the <c:choose> : (或<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>

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} (或者,如果您不需要有条件地渲染一堆标记,因此您只能在标记属性中检查它,那么您可以使用EL条件运算符${condition? valueIfTrue : valueIfFalse}) ${condition? valueIfTrue : valueIfFalse} : (${condition? valueIfTrue : valueIfFalse} :)

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

To learn more about those ${} things (the Expression Language , which is a separate subject from JSTL ), check here . (要了解有关这些${}事物的更多信息( 表达语言 ,这是与JSTL不同的主题), 请点击此处 。)

See also: (也可以看看:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...