java - JSP中IF条件的使用

标签 java jsp if-statement jstl

我有这条线

<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/

相关文章:

java - 使用 SwingWorker/Swing 跟踪 wget (bash) 的进度

java - 将文本字段值发送到不带表单标记的服务器

javascript - ajax 调用时引导模式按钮 onclick 不起作用

mysql - 在触发器mysql中添加if else语句时出错

C# Linq - 如果输入不等于任何字符串 []

java - 如何将 JLabel 文本放在 JTextField 上

java - JProgressBar 被不正确地压缩/调整大小

java - 如何以编程方式创建java文件

javascript - 自定义谷歌登录按钮并使用服务器数据库进行身份验证

java - if 条件在 selenium webdriver 中出现错误