在 JSTL 中添加两美元金额

AppSensei

我的目标是创建一个名为的变量

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

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

但是,如果您不想修改您的 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} 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章