JSTL c:ifタグで文字列を比較するサンプル

書式
c:if test="${変数名 == '対象文字列’}"
JSTLの<c:if>タグはEL式の等価演算子(==)で文字列比較できます。

使用例1
日本語の文字列を比較します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<c:if test="${user == 'yamada'}">
文字列が一致しました。
</c:if>
<c:if test="${user == 'yamada'}"> 文字列が一致しました。 </c:if>
<c:if test="${user == 'yamada'}">
  文字列が一致しました。
</c:if>

使用例2
数字の文字列を比較します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<c:if test="${count == '123'}">
文字列が一致しました。
</c:if>
<c:if test="${count == '123'}"> 文字列が一致しました。 </c:if>
<c:if test="${count == '123'}">
  文字列が一致しました。
</c:if>

 

JSTL

Posted by arkgame