Java JSP中IF条件的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20259252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of IF condition in JSP
提问by Santino 'Sonny' Corleone
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){}
file_name 是 mysql 数据库表中的列名。我想检查 file_name 是否有一些值,所以我想使用IF condition
,但我如何通过row.file_name
?就像是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
在这种情况下,即使 file_name 为空,也只执行第二个条件
采纳答案by Rohit Jain
First of all, if
is not a loop, it is just a statement. You can use <c:if>
tag for testing the value:
首先,if
不是循环,它只是一个语句。您可以使用<c:if>
标签来测试值:
<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>
):
而对于 Javaif-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>
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:
请注意,${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>
回答by NickDK
You should use the if statement from the JSTL Core library, just like you use c:out
您应该使用 JSTL Core 库中的 if 语句,就像使用 c:out 一样
<c:if test="${empty row.file_name}">File name is null or empty!</c:if>
回答by Masudul
Without <c:if/>
you can test file_name
is null
by using default
.
没有<c:if/>
你可以测试file_name
是 null
通过使用default
.
<td><c:out value="${row.file_name}" default="NULL FILE"/></td>