411

如何使用 的标签验证 aString是否为空或为空?cJSTL

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

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

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

4

8 回答 8

809

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

您可以为此使用emptya 中的关键字<c:if>

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

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

要了解有关这些${}内容的更多信息(表达式语言,它是与JSTL不同的主题),请查看此处

也可以看看:

于 2010-05-11T14:47:01.803 回答
27

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

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

它还处理空值

于 2013-05-20T09:54:48.937 回答
7

如果您只检查 null 或 empty 那么您可以为此使用 with default 选项: <c:out default="var1 is empty or null." value="${var1}"/>

于 2015-09-11T10:38:28.540 回答
6

此代码是正确的,但如果您输入大量空格 (' ') 而不是 null 或空字符串,则返回 false。

要更正此问题,请使用正则表达式(下面的代码检查变量是否为空、空或空白,与 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>
于 2012-12-13T07:25:06.080 回答
6

这是一个班轮。

EL 中的三元运算符

${empty value?'value is empty or null':'value is NOT empty or null'}
于 2015-02-06T07:36:58.347 回答
3

您可以使用

    ${var == null}

或者。

于 2016-11-30T09:28:47.387 回答
1

下面是一个如何验证从 Java 控制器传递到 JSP 文件的 int 和 String 的示例。

主控制器.java:

@RequestMapping(value="/ImportJavaToJSP")
public ModelAndView getImportJavaToJSP() {
    ModelAndView model2= new ModelAndView("importJavaToJSPExamples");

    int someNumberValue=6;
    String someStringValue="abcdefg";
    //model2.addObject("someNumber", someNumberValue);
    model2.addObject("someString", someStringValue);

    return model2;
}

importJavaToJSPExamples.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<p>${someNumber}</p>
<c:if test="${not empty someNumber}">
    <p>someNumber is Not Empty</p>
</c:if>
<c:if test="${empty someNumber}">
    <p>someNumber is Empty</p>
</c:if>
<p>${someString}</p>
<c:if test="${not empty someString}">
    <p>someString is Not Empty</p>
</c:if>
<c:if test="${empty someString}">
    <p>someString is Empty</p>
</c:if>
于 2017-04-12T01:19:29.710 回答
-1
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>
于 2015-07-28T07:12:34.603 回答