mathstruts2ceil

How to get ceil value of number when divided in <s:iterator> tag in Struts 2


I am using Struts <s:iterator> tag for displaying my array list, I had to put a condition in the end attribute of the <s:iterator> tag. How to get the ceil value when two integers are divided. If the array list size is 3 then I need to get the output as 2 for the end attribute.

<s:iterator var="counter" begin="0" end="arraylist.size()/2 " >
   /*my code...*/
</s:iterator> 

Solution

  • Because size method return type is int and the end attribute of <s:iterator> eventually will be cast to Integer you can simple add 1 to the size of array list before dividing it and you get same result as using Math.ceil.

    <s:iterator var="counter" begin="0" end="(arraylist.size()+1)/2">
    
    </s:iterator>
    



    BTW if you decide to enable struts.ognl.allowStaticMethodAccess which is not recommended and use Math.ceil method in JSP you need to indicate that you are dividing by a double value (e.g. 2d) or it will be converted to int before passing it to ceil.

    <s:iterator var = "counter" begin = "0" 
                end = "@java.lang.Math@ceil(arraylist.size()/2d)">
    
    </s:iterator>