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

Upvotes: 10

Views: 53939

Answers (3)

Rohit Jain
Rohit Jain

Reputation: 213213

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>

Upvotes: 11

Masudul
Masudul

Reputation: 21961

Without <c:if/> you can test file_name is null by using default.

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

Upvotes: 1

NickDK
NickDK

Reputation: 5197

You should use the if statement from the JSTL Core library, just like you use c:out

<c:if test="${empty row.file_name}">File name is null or empty!</c:if>

Upvotes: 1

Related Questions