Doubt with Java EL code snippet

Asked

Viewed 71 times

2

I’m studying a code and I don’t understand this part of EL:

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

The one that was referenced pedidoMesa, to appear the id and the nomeCliente? I turned the code around and found nothing to link it to Model.

  • 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.

  • 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 ?

1 answer

3

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.

  • So far so good, but I wouldn’t have to reference somehow the orderMesa with the Model Mesa, to call the orderMesa.id, for example ? This reference I didn’t find in the code.

  • @Fernando response edited.

  • ok, now I get it. Thanks for the help.

  • 1

    In fact, I’m pretty sure it’s translated to: System.out.println("Pedido " + pedidoMesa.getId() + " " + pedidoMesa.getNomeCliente());, but that’s just it. It really doesn’t need to associate the variable with the class.

  • @Dudaskank It was just an example p/ show that were only concatenating the attributes.

  • So, but if I don’t need to associate the variable with the class, I can use this variable with any Model class in the system ?

  • @Fernando as long as the object is in the scope of the page, request, session or Servlet, yes. I edited the reply :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.