【问题标题】:How to compare the value while accessing through forEach tag of JSTl通过JSTl的forEach标签访问时如何比较值
【发布时间】:2014-07-16 06:33:37
【问题描述】:
  <c:forEach var="healthp" items="${healthparam}" >
  <tr>
 <td><c:out value="${healthp.name}"/></td>
 <td><c:out value="${healthp.current_Reading}"/></td>
 <td><c:out value="${healthp.target}"/></td>
 <td><c:out value="${healthp.measurementunit}"/></td>
 <td><c:out value="${healthp.target}"/></td>
 <td><c:out value="${healthp.recorddate}"/></td>  
  </tr>
 </c:forEach>

在此我想将 Current_Reading 与目标进行比较。 如果当前读数具有更高的值,那么我只想在该 (Current_Reading) 列中显示,否则我想在目标列上显示它。 任何帮助表示赞赏

【问题讨论】:

    标签: java compare jstl


    【解决方案1】:

    您可以使用 c:if 标签。如果我对您的理解正确,并且您想隐藏 current_Reading 或 target 中的值(以较低者为准),那么它可能如下所示:

      <td><c:out value="${healthp.name}"/></td>
      <td>
        <c:if test="${healthp.current_Reading > healthp.target}">
          <c:out value="${healthp.current_Reading}"/>
        </c:if>
      </td>
      <td><c:out value="${healthp.target}"/></td>
      <td><c:out value="${healthp.measurementunit}"/></td>
      <td>
        <c:if test="${healthp.current_Reading <= healthp.target}">
          <c:out value="${healthp.target}"/>
        </c:if>
      </td>
      <td><c:out value="${healthp.recorddate}"/></td>
    

    如果这是您要查找的内容,这将为您留下一个空列。

    【讨论】:

    • 不错的答案。但看起来 OP 想要在这两种情况下都显示 Current_Reading 。但根据条件,OP 希望在不同的列中显示此值。
    • 谢谢,让我看看它是否有效
    【解决方案2】:

    在使用表达式语言时,您可以使用 eq 以及 ne、lt 等。

    <c:if test="${var1 eq var2}">some code</c:if>
    

    【讨论】:

      【解决方案3】:

      我相信您正在寻找这种技术。 “三元”运算符是一种在单列中显示两个值中较高者的简洁方式:

      <c:forEach var="healthp" items="${healthparam}" >
      <tr>
          <td><c:out value="${healthp.name}"/></td>
          <td><c:out value="${healthp.current_Reading > healthp.target ? healthp.current_Reading : healthp.target}"/></td>
          <td><c:out value="${healthp.measurementunit}"/></td>
          <td><c:out value="${healthp.target}"/></td>
          <td><c:out value="${healthp.recorddate}"/></td>  
      </tr>
      </c:forEach>
      

      旁注:大多数现代 JSP 实现不再需要 c:out,因此您可以通过这样做使您的代码更具可读性:

      <c:forEach var="healthp" items="${healthparam}">
      <tr>
          <td>${healthp.name}</td>
          <td>${healthp.current_Reading > healthp.target ? healthp.current_Reading : healthp.target}</td>
          <td>${healthp.measurementunit}</td>
          <td>${healthp.target}</td>
          <td>${healthp.recorddate}</td>  
      </tr>
      </c:forEach>
      

      【讨论】:

      • 谢谢,让我看看它是否有效
      猜你喜欢
      • 2017-02-18
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 2017-08-29
      相关资源
      最近更新 更多