【问题标题】:JSTL <c:forEach> not supports List<Integer>?JSTL <c:forEach> 不支持 List<Integer>?
【发布时间】:2011-01-04 18:33:24
【问题描述】:
                            ${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); 

我已经为此苦苦挣扎了一段时间,有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

  • 尝试删除" ${test}"中的空间。
  • 使用评估 ${} 符号本身的 jstl 版本添加您(如果我没记错的话,它是 jstl-rt),还是 jsp 为您评估它?

标签: java jsp jstl


【解决方案1】:

刚刚在我的应用程序中检查了它,因此将其作为答案:尝试从 items=" ${test}" 中删除空间。否则,您将执行与此 java 表达式 " " + testList 等效的操作。显然,结果不是列表。

3 次迭代中 ${sharedType} 的“[1”“2”“3”
很可能只有一次迭代,您会看到test.toString() 调用的结果。

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 2011-07-25
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 2013-08-18
    • 2012-07-15
    相关资源
    最近更新 更多