How to know if a List’s size in Struts2 tag

In my last project, I forgot to judge the list’s size before loop it. So I got an Exception. Then I googled the following solutions.

1. To know if it’s empty:

<s:if test="%{productList.isEmpty()}">
    //if the list is empty
</s:if>
<s:else>
    <s:iterator value="productList">
      //if not empty, then do the loop
    </s:iterator>
</s:else>

2. To know its size

<s:if test="%{productList.size() > 0}">
    //do the loop
</s:if>
The following two tabs change content below.

silentred

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>