在jsp中显示唯一值

时间:2014-07-01 23:52:37

标签: jsp

以下代码可以正常捕获所有值并显示它们,例如Live,Live,Live,Dead等。

<c:forEach var="instState" items="${inst.instanceState}" varStatus="loop">
<c:out value="${instState.userFriendlyString}"/>
<c:if test="${!loop.last}">,</c:if>
</c:forEach>

我想避免在页面上显示重复的值。我们有没有jsp标签?

2 个答案:

答案 0 :(得分:1)

没有这样的jsp标签。如果需要,您可以编写自定义标记。最简单的解决方案是将值复制到LinkedHashSet并迭代它

<jsp:useBean id="unique" class="java.util.LinkedHashSet" scope="request">
    <%
        unique.add("Live");
        unique.add("Live");
        unique.add("Live");
        unique.add("Dead");
    %>
    </jsp:useBean>

    <c:forEach var="instState" items="${unique}" varStatus="loop">
        <c:out value="${instState}"/>
        <c:if test="${!loop.last}">,</c:if>
    </c:forEach>

答案 1 :(得分:0)

感谢您的回复。 使用上面的代码,它显示我们添加为unique.add()的所有值,例如&#34; Live&#34;,&#34; Dead&#34;。事情是我从数据库中抓取结果,它只能是&#34; Live&#34;或者&#34;死了&#34;。所以,如果&#34;死&#34;在结果中出现然后我们只想显示它。

由于