Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of IF condition in JSP

I have this line

<td><c:out value="${row.file_name}"/></td>

file_name is a column name from the mysql database table. I want to check if file_name has some value,so I want to use the IF condition,but how do I pass row.file_name? something like if(row.file_name!=null){}

UPDATE

<td><c:out value="${row.file_name}"/><br>
<c:choose>
    <c:when test="${row.file_name == null}">
         Null
    </c:when>
    <c:otherwise>
       <a href="downloadFileServlet?id=${row.id}">Download</a></td>
    </c:otherwise>
</c:choose>

In this case only the 2nd condition is executed even though the file_name is empty

like image 458
Santino 'Sonny' Corleone Avatar asked Nov 28 '13 06:11

Santino 'Sonny' Corleone


People also ask

What is if condition example?

if (score >= 90) grade = 'A'; The following example displays Number is positive if the value of number is greater than or equal to 0 . If the value of number is less than 0 , it displays Number is negative .

What is the syntax for IF and ELSE condition?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to select one of many blocks of code to be executed.

What is expression in JSP?

A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client.


1 Answers

First of all, if is not a loop, it is just a statement. You can use <c:if> tag for testing the value:

<c:if test="${row.file_name != null}">
    Not Null   
</c:if>

And for Java if-else statement, JSTL tag equivalent is <c:choose>(No, there is no <c:else>):

<c:choose>
    <c:when test="${row.file_name != null}">
        Not Null
    </c:when>
    <c:otherwise>
        Null
    </c:otherwise>
</c:choose>

Note that, ${row.file_name != null} condition will be true only for non-null file name. And empty file name is not null. If you want to check for both null and empty file name, then you should use empty condition:

<!-- If row.file_name is neither empty nor null -->
<c:when test="${!empty row.file_name}">
    Not empty
</c:when>
like image 183
Rohit Jain Avatar answered Oct 04 '22 01:10

Rohit Jain