Maximum number of options within the c: choose tag of JSTL

0

Personally I was recently working with JSTL and I came across the following situation, I tried to add 3 tags when inside a choose tag and the last tag just does not work, the code looks like this:

<c:choose>
        <c:when test="${pagina != null}">
            <c:import url="${pagina}.jsp"></c:import>
        </c:when>
        <c:when test="${mensagem != null}">
            <br><p><c:out value="${mensagem}"></c:out></p>
        </c:when>
        <c:when test="${salvarTodosCertificados != null}">
            <p>Caso queira baixar todos os certificados em zip:</p> <br>
            Clique <a href="site?comando=gerarCertificado&quantos=todos">aqui</a>
        </c:when>
    </c:choose>

The maximum number of tags when I can put inside a choose tag is actually 2?

I had to replace the above code with this:

    <c:choose>
        <c:when test="${pagina != null}">
            <c:import url="${pagina}.jsp"></c:import>
        </c:when>
        <c:when test="${mensagem != null}">
            <br><p><c:out value="${mensagem}"></c:out></p>
        </c:when>
    </c:choose>
    <c:choose>
        <c:when test="${salvarTodosCertificados != null}">
            <p>Caso queira baixar todos os certificados em zip:</p> <br>
            Clique <a href="site?comando=gerarCertificado&quantos=todos">aqui</a>
        </c:when>
    </c:choose>

Does this proceed or set something up wrong or do I know?

    
asked by anonymous 31.12.2016 / 18:56

1 answer

1

The number of clauses within <c:choose> is, for practical purposes, unlimited.

However, it seems that you did not really understand the idea of <c:choose> . It works similarly to% w / o% full of% w / o% s.

In the case of the first code, what you did is something like this:

if (pagina != null) {
    // ...
} else if (mensagem != null) {
   // ...
} else if (salvarTodosCertificados != null) {
   // ...
}

The second one is more or less this:

if (pagina != null) {
    // ...
} else if (mensagem != null) {
   // ...
}
if (salvarTodosCertificados != null) {
   // ...
}

Note that each if clause is analogous to a else if or <c:when> . If you had if , it would be analogous to else if .

In a <c:otherwise> structure, only the first else that has the satisfied condition executes. Even if any of the% s of lower% s could also be satisfied, only the first one that is satisfied performs. The same happens with if ... else if ... else if ... .

So your problem seems to be that you expected if of the if condition to be executed even when one of the conditions of one of the <c:choose> s above was also, which will not happen because of fact that only the first for which the condition is true is performed. Or, you hoped the first two would be false when one of them is true. Or, you put your <c:when> in the wrong order.

    
31.12.2016 / 19:56