【问题标题】:Evaluate empty or null JSTL c tags评估空或空 JSTL c 标签
【发布时间】:2011-02-18 04:35:00
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: jsp jstl el


    【解决方案1】:

    这是一个班轮。

    EL 中的三元运算符

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我的评论有什么问题?
      【解决方案3】:

      你可以使用

          ${var == null}
      

      或者。

      【讨论】:

      • 不,很遗憾,您不能。没有任何符号的“”是一个空字符串,但不为空。
      【解决方案4】:

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

      您可以为此在&lt;c:if&gt; 中使用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>
      

      或者&lt;c:choose&gt;:

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

      要了解有关${} 的更多信息(Expression Language,这是与JSTL 不同的主题),check here。

      另见:

      【讨论】:

      • 对于那些对空支票有奇怪问题的人,这里有一个可能原因的可疑故事:gayleforce.wordpress.com/2008/01/26/jstl-empty-operator
      • 总结:empty 在使用古老的 JSTL 1.0 时不适用于 Set。您需要升级到 JSTL 1.1(从 2003 年开始)。
      • @BalusC - EL ${not empty var1} 是否同时检查空和空?我的意思是当且仅当 var1 为 not null 并且 var1 为 not 空时,测试才被评估为 true。 null不用单独查吗?
      • 是 empty 相当于 ne ' '
      • @shareef:不,不是。对于 String 值,它等效于 var ne null and var ne ''。此外,它还支持Object、数组、Collection 和Map。
      【解决方案5】:

      如果您只检查 null 或空,那么您可以使用 with default 选项: &lt;c:out default="var1 is empty or null." value="${var1}"/&gt;

      【讨论】:

        【解决方案6】:

        还要检查空白字符串,我建议关注

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

        它也处理空值

        【讨论】:

          【解决方案7】:
          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>
          

          【讨论】:

            【解决方案8】:

            此代码是正确的,但如果您输入了很多空格 (' ') 而不是 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>
            

            【讨论】:

              猜你喜欢
              • 2021-11-03
              • 2013-08-17
              • 2016-03-19
              • 1970-01-01
              • 1970-01-01
              • 2015-01-18
              • 1970-01-01
              • 2011-09-05
              相关资源
              最近更新 更多