1

我的 Spring MVC 应用程序中的 JSTL 的 forEach-tag 有问题。

我想用对象迭代一个简单的 LinkedList。在这里,您可以从 .jsp 文件中看到我的代码:

    <c:forEach items="${posts}" var="single_post">
        <div id="post">
            <label><c:out value="${single_post.topic}" /></label> <br />
            <div id="post_content">
                <c:out value="${single_post.content}" />
            </div>
            <div id="post_information">
                <c:out value="${single_post.username}" />
                |
                <c:out value="${single_post.dayOfCreation}" />
            </div>
            <div id="post_tags">
                <c:out value="${single_post.tags}" />
            </div>
            <hr />
        </div>
    </c:forEach>

forEach-tag 遍历整个列表,但只返回第一个元素。我用一个简单的例子在另一个案例上尝试了它,但没有 LinkedList。

<c:forEach var="i" begin="1" end="5">
    Item <c:out value="${i}"/>
</c:forEach>

这个例子工作正常,所以我不知道问题出在哪里。

此致 :)

更新 1: 生成列表的代码是一个简单的 sql 语句。语句的结果被添加到 LinkedList。

在我的控制器中,我有以下方法将 sql 语句的结果添加到模型中:

@RequestMapping(value = "/community", method = RequestMethod.GET)
    public String community(Locale locale, Model model) {

    // Add attributes to the model
    model.addAttribute("posts", appService.getRelatedPosts());

    return "/community";
}

forEach-tag 的结果如下所示:

测试名称Bespieltext TestAccount |
Tag_1 、Tag2、Tag_3 测试名称
Tag_1 、Tag2、Tag_3 测试名称 标记_1、标记2、标记_3。. .

是的,数据库中有不同的值;)

4

0 回答 0