Doubt with Java code snippet EL [closed]

2

I'm studying a code and I'm not understanding this excerpt from EL:

<c:choose>
    <c:when test="${not empty pedidoMesa}">
        Pedido: ${pedidoMesa.id} ${pedidoMesa.nomeCliente}  
    </c:when>
    <c:otherwise>
       Mesa
    </c:otherwise>
</c:choose>

As this pedidoMesa was referenced, to show id and nomeCliente ? I turned and rolled the code and found nothing that could relate to Model.

    
asked by anonymous 20.07.2017 / 20:04

1 answer

3

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 .

    
20.07.2017 / 21:23