Search code examples
javajspstruts2ognlvaluestack

How to get the sum of values inside Struts 2 iterator?


My application is shopping website application. In place order case, I need to pass the sum of every item's price as a one parameter. Prices are coming to the iterator correctly. But I failed to get the sum of those prices. shopping cart list is coming from Another action class.

I tried with this:

<s:iterator value="shoppingCarts" var="shoppingCarts">      
   <s:label name="total" value="%{#total+(item.salesPrice*qty)+item.shippingCost}" label="Total"/>
</s:iterator>

This shows two labels, if shopping cart list has two items. How can I get a total to a one parameter inside the iterator?


Solution

  • Define a context variable total like in this exampe

    <s:set var="total" value="0">
    <s:iterator value="shoppingCarts">
      s:set var="total" value="%{#total+item.salesPrice*qty+item.shippingCost}"/>         
    </s:iterator>
    <s:label name="total" value="%{#total}" label="Total"/>