在 JSTL 中添加两美元金额

Adding two dollars amount in 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)。 您可以在您的帖子中添加一个字段 class 并相应地写入 return 值。

//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}