Briefly:
<c:choose>
in expression language is like a switch/case
in your Java code.
The <when test="{condição}">
is like a case "condição":
.
And the <c:otherwise>
is equivalent to default
within a structure switch/case
.
Note that the core JSTL does not have a condition <c:else>
, that is, if you need to test a condition and from it 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>
There is no problem writing the code like this, but some developers prefer to use the <c:choose>
(which is equivalent to switch/case
) and make use of <c:otherwise>
to treat the contrary condition within the 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 with name will be returned pedidoMesa
which is not null, no matter in what context the request was created/defined. However in the following order: PageContext
, HttpServletContext
, HttpSession
and ServletContext
.
Blz Fernando, could you elaborate on your question? Because the above code is simple, the requested object is in Scope (request, Session, etc), and you can refer via EL normally, the id and client name, must be accessible in this object.
– Caio
My question is as follows, I wouldn’t have to refer to the Order Model for example ? I didn’t find this reference. How does the system know that this request refers to this Model ?
– Fernando