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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:50:54  来源:igfitidea点击:

How to know if List is empty or not in Struts2?

liststruts2

提问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 productListLike

使用 ApaetproductList.isEmpty()您还可以检查productListLike的大小

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