Vijay Rahul
Vijay Rahul

Reputation: 103

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> 

Upvotes: 1

Views: 973

Answers (3)

Aleksandr M
Aleksandr M

Reputation: 24396

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> 

Upvotes: 0

Roman C
Roman C

Reputation: 1

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.

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50193

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()) .

Upvotes: 0

Related Questions