Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

list

struts2

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.

<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>   
like image 905
Rohit Kachhadiya Avatar asked Jan 17 '23 14:01

Rohit Kachhadiya


2 Answers

You can use Struts2 <s:if> and <s:else> tags for conditional checking like this:

<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>
like image 195
mprabhat Avatar answered Jan 19 '23 03:01

mprabhat


<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>
like image 39
pbaris Avatar answered Jan 19 '23 04:01

pbaris