How to find out if List is empty or not in Struts2?

I have one List. This is a selection from the java class to the jsp page. I want to display this list on the jsp page, but if List is empty, then display one error message, otherwise display the list item.

<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>   
+3
source share
4 answers

You can use the Struts2 <s:if>and tags <s:else>for conditional validation as follows:

<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>
+6
source
<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>
+2
source

Apaet of productList.isEmpty()you can also check the size of productList how

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

You can also use 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>
0
source

All Articles