代码之家  ›  专栏  ›  技术社区  ›  AppSensei

在JSTL中添加两美元的金额

  •  0
  • AppSensei  · 技术社区  · 6 年前

    我的目标是做一个变量

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

    1 回复  |  直到 6 年前
        1
  •  2
  •   ST. Kee    6 年前

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

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

    然后简单地引用它:

    ${posting.dollarDiff}
    

    如果方法遵循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}