Question

I am using struts iterator for displaying my array list I had to put a condition in the end attribute of the iterator tag. how to get the CEIL value when two integers are divided. if array list size is 3 then I need to get output as 2 for end attribute

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

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> 

OTHER TIPS

In struts.xml

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 

Now it is possible to call static methods; the syntax, as described here, would be:

@java.lan.Math@ceil()

and then

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

</s:iterator> 

Obviously you need in the action a property named like that (the naming choice is no good at all, if it is the real name)

private List<Object> arraylist;

with obviously its getter (getArraylist()) .

When two integers are divided you get a rational number. So, to get rid of the fractional part you can cast it to int.

int length = (int) arraylist.size()/2;

Use this result in your iterator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top