${test}
<c:forEach items=" ${test}" var="sharedType">
${sharedType}<br/>
<c:if test="${sharedType == 2}" >
<span class="tweet-button"></span>
</c:if>
<c:if test="${sharedType == 1}" >
TEST
<a href="#" class="fbshare-button">Share</a>
</c:if>
</c:forEach>
整数列表是1,2,3。 在我的页面上,它在 3 次迭代中显示 ${test} 的 [1,2,3] 和 ${sharedType} 的“[1”“2”“3”。看起来 JSTL 认为它是一个用逗号分隔的字符串而不是一个列表。 java中生成列表的代码是:
List<Integer> test= new ArrayList<Integer>();
test.add(1);
test.add(2);
test.add(3);
我已经被这个问题困扰了一段时间了,谁能帮我解决这个问题吗?谢谢。
最佳答案
刚刚在我的应用程序中检查了它,因此将其作为答案:尝试从 items="${test}"
中删除空格。否则,您将执行与此 java 表达式 ""+ testList
等效的内容。显然,结果不是一个列表。
${sharedType} 的“[1”“2”“3”迭代 3 次
很可能只有一次迭代,您将看到 test.toString()
调用的结果。
关于java - JSTL <c :forEach> not supports List<Integer>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597073/