在 JSTL 中添加两美元的金额



我的目标是制作一个名为

"Dollar Diff" = Value of posting.dollarsInHeader -posting.dollarsReceived)/1000000

检查下面的代码

<c:choose>
    <c:when test="${posting.dollarsInHeader != 0 || posting.dollarsReceived != 0}">
        <td class="alignright" class="${posting.dollarsInHeader < 0 || posting.dollarsReceived < 0 ? 'fontRed' : ''}">
            <fmt:formatNumber type="currency" minFractionDigits="1"
                                            maxFractionDigits="1">${(posting.dollarsInHeader - posting.dollarsReceived)/1000000}
        </td>
    </c:when>
    <c:otherwise>
        <td style="text-align: right; padding-right: 10px;">-</td>
    </c:otherwise>
</c:choose>

与其写${(posting.dollarsInHeader - posting.dollarsReceived)/1000000},不如写${dollarDiff}

我不建议用视图层(jsp(编写这样的逻辑。 您可以在发布类中添加一个字段,并相应地写入返回值。

//Ommit Posting class declaration
public double getDollarDiff(){
    return (this.dollarsInHeader-this.dollarsReceived)/1000000;
}

然后只需引用它:

${posting.dollarDiff}

EL 将您的方法视为一个字段,如果它遵循 getter 约定。

但是,如果您不想修改您的 pojo,您可以尝试使用

<c:set scope="request" var="dollarDiff" value="${(posting.dollarsInHeader - posting.dollarsReceived)/1000000}"></c:set>

然后引用它:

<c:out value="${requestScope.dollarDiff}"></c:out> 
<!--or-->
${requestScope.dollarDiff} 

最新更新