Using the IF Condition in JSP
I have this line
<td><c:out value="${row.file_name}"/></td> filename is the name of the column from the mysql database table. I want to check if file_name has any meaning, so I want to use IF condition , but how to get through 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 second condition is true, even if the file_name is empty
First of all, if not a loop, it is just an operator. You can use the <c:if> to check the value:
<c:if test="${row.file_name != null}"> Not Null </c:if> And for the Java if-else equivalent of the JSTL tag is <c:choose> (No, 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 the condition ${row.file_name != null} will be true only for the non-null file name. And the empty file name is not zero. If you want to check both null and the empty file name, you should use the empty condition:
<!-- If row.file_name is neither empty nor null --> <c:when test="${!empty row.file_name}"> Not empty </c:when> 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> Without <c:if/> you can check file_name for null with default .
<td><c:out value="${row.file_name}" default="NULL FILE"/></td>