list 如何知道Struts2中的List是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10714765/
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
How to know if List is empty or not in Struts2?
提问by Rohit Kachhadiya
I have one List. Which is fetch from java class to jsp page. I want to display this List in jsp page but, if List is empty then display one error message otherwise display List's item.
我有一个清单。这是从java类到jsp页面的提取。我想在jsp页面中显示此列表,但是,如果列表为空,则显示一条错误消息,否则显示列表的项目。
<s:iterator value="productList">
<tr style="background-color: #99CCFF">
<td><s:property value="pid"/></td>
<td><s:property value="productname"/></td>
<td><s:property value="producttype"/></td>
<td><s:property value="productprice"/></td>
<td><s:property value="shopname"/></td>
<td><s:property value="productcity"/></td>
<td><s:property alue="ownername"/></td>
</tr>
</s:iterator>
回答by mprabhat
You can use Struts2 <s:if>
and <s:else>
tags for conditional checking like this:
您可以使用 Struts2<s:if>
和<s:else>
标签进行条件检查,如下所示:
<s:if test="%{getProductList().isEmpty()}">
Error
</s:if>
<s:else>
<s:iterator value="productList">
<tr style="background-color: #99CCFF">
<td><s:property value="pid"/></td>
<td><s:property value="productname"/></td>
<td><s:property value="producttype"/></td>
<td><s:property value="productprice"/></td>
<td><s:property value="shopname"/></td>
<td><s:property value="productcity"/></td>
<td><s:property alue="ownername"/></td>
</tr>
</s:iterator>
</s:else>
回答by pbaris
<s:if test="%{productList.isEmpty()}">
<tr>
<td colspan="7">Empty</td>
</tr>
</s:if>
<s:else>
<s:iterator value="productList">
<tr style="background-color: #99CCFF">
<td><s:property value="pid"/></td>
<td><s:property value="productname"/></td>
<td><s:property value="producttype"/></td>
<td><s:property value="productprice"/></td>
<td><s:property value="shopname"/></td>
<td><s:property value="productcity"/></td>
<td><s:property alue="ownername"/></td>
</tr>
</s:iterator>
</s:else>
回答by xrcwrn
Apaet from using productList.isEmpty()
you can also check the size of productList
Like
使用 ApaetproductList.isEmpty()
您还可以检查productList
Like的大小
<s:if test="%{productList.size>0}">
<table>
<s:iterator value="productList">
<tr style="background-color: #99CCFF">
<td><s:property value="pid"/></td>
<td><s:property value="productname"/></td>
<td><s:property value="producttype"/></td>
<td><s:property value="productprice"/></td>
<td><s:property value="shopname"/></td>
<td><s:property value="productcity"/></td>
<td><s:property alue="ownername"/></td>
</tr>
</s:iterator>
</table>
</s:if>
<s:else>
<div> No data found</div>
</s:else>
回答by Jos
You can also this shorter syntax
您也可以使用更短的语法
<s:if test="productList.empty">
<tr>
<td colspan="7">Empty</td>
</tr>
</s:if>
<s:else>
<s:iterator value="productList">
<tr style="background-color: #99CCFF">
<td><s:property value="pid"/></td>
<td><s:property value="productname"/></td>
<td><s:property value="producttype"/></td>
<td><s:property value="productprice"/></td>
<td><s:property value="shopname"/></td>
<td><s:property value="productcity"/></td>
<td><s:property alue="ownername"/></td>
</tr>
</s:iterator>
</s:else>