JSTL c:if条件式で変数が空文字またはnullを判定するサンプル

書式1
c:if test="${empty 変数名}
emptyを使って、変数が空文字またはNULLかどうかを判定します。

使用例1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<c:if test="${empty username}">
変数名usernameはemptyまたはnullです
</c:if>
<c:if test="${empty username}"> 変数名usernameはemptyまたはnullです </c:if>
<c:if test="${empty username}">
   変数名usernameはemptyまたはnullです
</c:if>

書式2
c:if test="${not empty 変数名}"
not emptyを使って、変数が空文字またはNULLではないかどうかを判定します。

使用例2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<c:if test="${not empty username}">
変数名usernameは空文字またはNULLではないです.
</c:if>
<c:if test="${not empty username}"> 変数名usernameは空文字またはNULLではないです. </c:if>
<c:if test="${not empty username}">
   変数名usernameは空文字またはNULLではないです.
</c:if>

書式3
c:if test="${!empty 変数名}"
!emptyを使って、変数が空文字またはNULLではないかどうかを判定します。

使用例3

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<c:if test="${!empty username}">
変数名usernameは空文字またはNULLではないです.
</c:if>
<c:if test="${!empty username}"> 変数名usernameは空文字またはNULLではないです. </c:if>
<c:if test="${!empty username}">
  変数名usernameは空文字またはNULLではないです.
</c:if>

 

JSTL

Posted by arkgame