【问题标题】:Common page for view list or single object查看列表或单个对象的公共页面
【发布时间】:2012-01-17 10:08:23
【问题描述】:

我想写一个通用页面来查看数据列表或单个对象。

在控制器中:

ArrayList<Table> tables;
request.setAttribute("tables", tables);

和 JSP:

<table >
    <c:forEach var="table" items="${tables}">
        <tr>
            <th><c:out value=" ${table.name}" /></th>
        </tr>
    </c:forEach>
</table>

显示不错。

当我传递单个对象时 -

Table table;
request.setAttribute("table", table);

什么都不显示;

比我尝试通过

Table tables;
request.setAttribute("tables", tables);

我有错误

Don't know how to iterate over supplied "items" in &lt;forEach&gt;

这是一个非常简单的例子,我的应用程序必须查看这个对象的大量数据,并使用相同的代码制作两个相似的页面。 如何解决这个问题呢?将此单个对象添加到列表中还是有其他解决方案?

【问题讨论】:

  • 你应该把那个单一的“表”对象添加到一个列表中。

标签: jsp servlets jstl


【解决方案1】:

您似乎想传递单个元素而不是列表。解决此问题的一种方法是使用单个元素构建一个列表并传递它。使用以下创建列表,其中只有一个元素。

List<Table> tables = Collections.singletonList(table);

【讨论】:

    【解决方案2】:

    这是因为 jstl forEach 标记需要一个列表,而您传递的是一个对象。 尝试传递一个元素列表:

    Table table;
    ArrayList<Table> tables=new ArrayList<Table>();
    tables.add(table)
    request.setAttribute("tables", tables);
    

    【讨论】:

    • 感谢您的回答,但我会效仿哈里·乔伊的例子。
    • @Viachaslau 确实,我也愿意。它比我的干净得多:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多