Java 如何在通过JSTl的forEach标记访问时比较值

Java 如何在通过JSTl的forEach标记访问时比较值,java,compare,jstl,Java,Compare,Jstl,在这里,我想将当前的_读数与目标进行比较。 如果当前读数具有更高的值,则只有我想在该当前读数列中显示,否则我想在目标列中显示。 非常感谢您的帮助您可以使用c:if标记。如果我理解正确,并且您希望在当前_读数或目标值中隐藏该值(以较低者为准),则它可能如下所示: <c:forEach var="healthp" items="${healthparam}" > <tr> <td><c:out value="${healthp.name}"/>

在这里,我想将当前的_读数与目标进行比较。 如果当前读数具有更高的值,则只有我想在该当前读数列中显示,否则我想在目标列中显示。
非常感谢您的帮助

您可以使用c:if标记。如果我理解正确,并且您希望在当前_读数或目标值中隐藏该值(以较低者为准),则它可能如下所示:

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

如果您正在寻找的是一个空列,那么这将留给您一个空列。

您可以使用c:if标记。如果我理解正确,并且您希望在当前_读数或目标值中隐藏该值(以较低者为准),则它可能如下所示:

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

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

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

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

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

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

我相信这种技术正是你所需要的。三元运算符是在单个列中显示两个值中较高值的干净方法:

<c:if test="${var1 eq var2}">some code</c:if>
旁注:大多数现代JSP实现不再需要c:out,因此您可以通过执行以下操作来提高代码的可读性:

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

我相信这种技术正是你所需要的。三元运算符是在单个列中显示两个值中较高值的干净方法:

<c:if test="${var1 eq var2}">some code</c:if>
旁注:大多数现代JSP实现不再需要c:out,因此您可以通过执行以下操作来提高代码的可读性:

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

回答得好。但OP似乎希望在这两种情况下显示当前的_读数。但基于条件,OP希望在不同的列中显示此值。谢谢,让我检查一下它是否有效。回答很好。但OP似乎希望在这两种情况下显示当前的_读数。但是根据条件,OP希望在不同的列中显示此值。谢谢,让我检查它是否工作谢谢,让我检查它是否工作谢谢,让我检查它是否工作