Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do "nested if" in JSTL for Java JSP?

Tags:

java

jsp

jstl

I want to do something like the following:

<c:choose>
    <c:when test="${empty example1}">
    </c:when>
    <c:otherwise>
        <c:when test="${empty example2}">
        </c:when>
        <c:otherwise>
        </c:otherwise>              
    </c:otherwise>
</c:choose>

Is this even possible? I get an exception thrown when trying to run.

Thank you.

like image 558
user678616 Avatar asked Mar 27 '11 04:03

user678616


People also ask

What is the difference between JSP and JSTL?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).

What is the use of JSTL tags in JSP?

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc.

What does C out do in JSP?

c:out escapes HTML characters so that you can avoid cross-site scripting.

Which of these is a valid JSTL if tag?

The < c:if > tag is used for testing the condition and it display the body content, if the expression evaluated is true. It is a simple conditional tag which is used for evaluating the body content, if the supplied condition is true.


1 Answers

You need to do it more like this:

<c:choose>
    <c:when test="${empty example1}"> 
        <!-- do stuff -->
    </c:when> 
    <c:otherwise> 
        <c:choose>
            <c:when test="${empty example2}"> 
                <!-- do different stuff -->
            </c:when> 
            <c:otherwise> 
                <!-- do default stuff -->
            </c:otherwise>
        </c:choose>
    </c:otherwise> 
</c:choose>

The verbosity shown here is a good example of why XML is a poor language for implementing multi-level conditional statements.

like image 143
aroth Avatar answered Sep 22 '22 18:09

aroth