Search code examples
javajspstruts2ognlstruts-tags

Calculation with <s:property /> values in Struts2


I have the following code:

<s:property value="currentPrice" />
<s:property value="oldPrice" />
<span class="badge">-50%</span>

Now I want to display the percentage in the <span> tag by the formula:

percentage = (currentPrice - oldPrice) / oldPrice * 100

How can I do that in Struts2 ?


Solution

  • Client side solution:

    <s:property value="currentPrice" />
    <s:property value="oldPrice" />
    <span class="badge">
        <s:property value="%{((currentPrice - oldPrice) / oldPrice) * 100}" />%
    </span>
    

    Or in alternative,

    Server side solution:

    public Integer getPercentage(){
        return ((currentPrice - oldPrice) / oldPrice) * 100;
    }
    
    <s:property value="currentPrice" />
    <s:property value="oldPrice" />
    <span class="badge">
        <s:property value="percentage" />%
    </span>