In summary:
<c:choose>
in expression language is like a switch/case
in your Java code.
The <when test="{condição}">
is like case "condição":
.
And <c:otherwise>
is equivalent to default
within a switch/case
structure.
Notice that the core of JSTL does not have a <c:else>
condition, ie if you need to test a condition and then take an action in case it is false, you would have to do something like this:
<c:if test="${not empty pedidoMesa}">
Pedido: ${pedidoMesa.id} ${pedidoMesa.nomeCliente}
</c:if>
<c:if test="${empty pedidoMesa}">
Mesa
</c:if>
It's okay to write code like this, but some developers prefer to use <c:choose>
(which is equivalent to switch/case
) and make use of <c:otherwise>
to treat the opposite condition within a same structure. The above code could be written like this:
<c:choose>
<c:when teste="${not empty pedidoMesa}">
Pedido: ${pedidoMesa.id} ${pedidoMesa.nomeCliente}
</c:when>
<c:otherwise>
Mesa
</c:otherwise>
</c:choose>
When you use ${pedidoMesa}
, the first attribute named pedidoMesa
that is not null will be returned, no matter in what context the request was created / defined. However, in the following order: PageContext
, HttpServletContext
, HttpSession
and ServletContext
.