What Is the Difference Between and?

I've noticed usages of <c:if ...> in one place of JSP code and <c:when ...> in the other. The things they do look the same for me. Are these two commands just aliases?

2 Answers

<c:if is a simple if-clause. <c:when> has options for multiple if-clauses and an else clause. Compare:

<c:if test="${foo == 'bar'}">...</c:if>

with

<c:choose>
   <c:when test="${foo == 'bar'}">...</c:when>
   <c:when test="${foo == 'baz'}">...</c:when>
   <c:otherwise>...</c:otherwise>
</c:choose>

<c:if> doesn't support any kind of "else" or "else if" functionality. <c:when> does. So if you need something analogous to

if (some_condition) {
    // ...
}

then use <c:if>. If you need something analogous to

if (some_condition) {
    // ...
} else if (some_other_condition) {
    // ...
} else {
    // ...
}

then use <c:choose> with <c:when> and (optionally) <c:otherwise>.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest