java - 省略 JSTL 中的最后一个逗号 <c :out>

标签 java jsp jstl

我查看了该网站上以前的示例,但没有任何效果。我也用谷歌搜索了我的困境,但没有成功。

我正在尝试在这里调试一些代码。我似乎可以去掉最后一个逗号。想法?

<c:forEach items="${userDeptList}" var="userDeptVar" varStatus="userDeptCounter" >
   <c:if test="${contactsVar.userId==userDeptVar.userID}">
      <c:forEach items="${deptPtypeList}" var="deptsVar" varStatus="deptsCounter" > 
         <c:if test="${deptsVar.ptypeID==userDeptVar.deptID}">
            <c:out value="${deptsVar.type}" escapeXml="false"></c:out>
            <c:out value="," escapeXml="false"></c:out> 
         </c:if>    
      </c:forEach>                          
   </c:if>                  
</c:forEach>

最佳答案

要省略最后一个逗号,您可以通过这种方式测试是否位于最后一项

<c:forEach items="${userDeptList}" var="userDeptVar" varStatus="userDeptCounter" >
   <c:if test="${contactsVar.userId==userDeptVar.userID}">
      <c:forEach items="${deptPtypeList}" var="deptsVar" varStatus="deptsCounter" > 
         <c:if test="${deptsVar.ptypeID==userDeptVar.deptID}">
            <c:out value="${deptsVar.type}" escapeXml="false"></c:out>
            <c:if test="${not userDeptCounter.last}>
              <c:out value="," escapeXml="false"></c:out> 
            </c:if>
         </c:if>    
      </c:forEach>                          
   </c:if>                  
</c:forEach>

这里是所有varStatus属性的列表

current    The item (from the collection) for the current round of iteration
index      The zero-based index for the current round of iteration
count      The one-based count for the current round of iteration
first      Flag indicating whether the current round is the first pass through the iteration
last       Flag indicating whether the current round is the last pass through the iteration
begin      The value of the begin attribute
end        The value of the end attribute
step       The value of the step attribute

关于java - 省略 JSTL 中的最后一个逗号 <c :out>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10179511/

相关文章:

java - 不明白 javac 对 -classpath 的以下使用

java - 如何在JSP文件中按自定义顺序显示EnumSet?

java - 使用 fmt :message in JavaScript 读取属性文件时出现引用问题

java - 如何为多种屏幕尺寸设置图像 - android

java - JDBC 通用查询执行

java - 在 Struts 2 jQuery 网格中 select 的格式化程序中获取所选值

jakarta-ee - 如何访问c :forEach with a scriptlet/expression?中的迭代变量

jsp - c :forEach Iteration

java - 使用固定字节数的java读取文件

java - 在 webapp 外部的 jsp 上显示图像