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