我有这条线
<td><c:out value="${row.file_name}"/></td>
file_name 是来自 mysql 数据库表的列名。
我想检查 file_name 是否有一些值,所以我想使用 IF 条件
,但是如何传递 row.file_name
?
类似于 if(row.file_name!=null){}
更新
<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>
在这种情况下,即使 file_name 为空,也只执行第二个条件
最佳答案
首先,if
不是循环,它只是一个语句。您可以使用 <c:if>
用于测试值的标记:
<c:if test="${row.file_name != null}">
Not Null
</c:if>
对于 Java if-else
声明,JSTL 标记等效为 <c:choose>
(不,没有 <c:else>
):
<c:choose>
<c:when test="${row.file_name != null}">
Not Null
</c:when>
<c:otherwise>
Null
</c:otherwise>
</c:choose>
请注意,${row.file_name != null}
条件将是 true
仅适用于 non-null
文件名。并且空文件名不为空。如果你想同时检查 null
和空文件名,那么你应该使用 empty
条件:
<!-- If row.file_name is neither empty nor null -->
<c:when test="${!empty row.file_name}">
Not empty
</c:when>
关于java - JSP中IF条件的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20259252/