Highlight text in JSTL

扶醉桌前 提交于 2019-12-25 03:57:42

问题


I need to display text using JSTL. There are two arrays.

array a [one, two, three, nine] array b [nine, one, two]

The displayed text should bold the elements in a that are also in b and leave the rest normal

one, two, three, nine

<c:forEach var="i" items="${a}">
              <c:forEach var="j" items="${b}">
                  <c:choose>
                      <c:when test="${i==j}">
                          <strong><c:out value="${i}"/></strong>
                      </c:when>
                      <c:when test="${i!=j}">
                           <c:out value="${i}"/>
                      </c:when>
                  </c:choose>
              </c:forEach>
          </c:forEach>

What am i missing? The code displays one highlighted and then normal 3 times


回答1:


The second <c:when> should be a <c:otherwise>.

You probably meant to put the <strong> and </strong> around one of the <c:out>.

Also, your code would output 4 * 3 = 12 values. Is that what you intended?
If not, perhaps changing b to a List or Set, and using b.contains(i) would be what you want.


Update

If b is a Collection, preferably a Set, then this will do:

<c:forEach var="i" items="${a}">
    <c:choose><c:when test="${b.contains(i)}">
        <strong><c:out value="${i}"/></strong>
    </c:when><c:otherwise>
        <c:out value="${i}"/>
    </c:otherwise></c:choose>
</c:forEach>


来源:https://stackoverflow.com/questions/35022587/highlight-text-in-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!