如何使用JSTL
的c
標籤驗證String
是空仍是空? javascript
我有一個名爲var1
的變量,我能夠顯示它,但我想添加一個比較器來驗證它。 html
<c:out value="${var1}" />
我想驗證它是null仍是空(個人值是字符串)。 java
這段代碼是正確的可是若是你輸入了不少空格('')而不是null或空字符串返回false。 apache
要更正此問題,請使用常規表達式(下面的代碼檢查變量是null仍是空或空白與org.apache.commons.lang.StringUtils.isNotBlank相同): oracle
<%@ 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>
還要檢查空白字符串,我建議以下 jsp
<%@ 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>
它還處理空值 this
如何使用JSTL的c標籤驗證String是空仍是空? spa
您能夠在<c:if>
使用empty
關鍵字: code
<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>
: htm
<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不一樣的主題), 請點擊此處 。
這是一個班輪。
${empty value?'value is empty or null':'value is NOT empty or null'}
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>