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: (也可以看看:)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…