评估空或空JSTL c标记

ぐ巨炮叔叔 提交于 2020-02-27 09:03:21

如何使用JSTLc标签验证String是空还是空?

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

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

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


#1楼

这段代码是正确的但是如果你输入了很多空格('')而不是null或空字符串返回false。

要更正此问题,请使用常规表达式(下面的代码检查变量是null还是空或空白与org.apache.commons.lang.StringUtils.isNotBlank相同):

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
        <c:if test="${not empty description}">
            <c:set var="description" value="${fn:replace(description, ' ', '')}" />
            <c:if test="${not empty description}">
                  The description is not blank.
            </c:if>
        </c:if>

#2楼

还要检查空白字符串,我建议如下

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:if test="${empty fn:trim(var1)}">

</c:if>

它还处理空值


#3楼

如何使用JSTL的c标签验证String是空还是空?

您可以在<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>

<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} ${condition? valueIfTrue : valueIfFalse}

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

要了解有关这些${}事物的更多信息( 表达语言 ,这是与JSTL不同的主题), 请点击此处

也可以看看:


#4楼

这是一个班轮。

EL内部的三元运算符

${empty value?'value is empty or null':'value is NOT empty or null'}

#5楼

In this step I have Set the variable first:

<c:set var="structureId" value="<%=article.getStructureId()%>" scope="request"></c:set>

In this step I have checked the variable empty or not:

 <c:if test="${not empty structureId }">
    <a href="javascript:void(0);">Change Design</a>
 </c:if>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!