【问题标题】:Unwrapping a Kotlin hashmap in Thymeleaf inside of Spring Boot 2在 Spring Boot 2 中的 Thymeleaf 中展开 Kotlin 哈希映射
【发布时间】:2018-01-19 21:06:05
【问题描述】:

我有一个 Kotlin 函数,它使用如下所示的哈希图创建模型

@GetMapping("/")
fun index(model: Model): Mono<String> {
    model.addAttribute("images", imageService.findAllImages()?.flatMap { image ->
        Mono.just(image)
            .zipWith(repository.findByImageId(image?.id!!).collectList())
            .map({ imageAndComments: Tuple2<Image?, MutableList<learningspringboot.images.Comment>> ->
                hashMapOf<String, Any?>(
                    "id" to imageAndComments.t1?.id,
                    "name" to imageAndComments.t1?.name,
                    "comments" to imageAndComments.t2)
            }).log("findAllImages")
    })

    model.addAttribute("extra", "DevTools can also detech code changes.")
    return Mono.just("index")
}

Image.kt

package learningspringboot.images

import org.springframework.data.annotation.Id

data class Image(@Id var id: String? = null, var name: String? = null)

Comment.kt

package learningspringboot.images

import org.springframework.data.annotation.Id

data class Comment @JvmOverloads constructor(@Id private var id: String? = null, private var imageId: String? = null, private var comment: String? = null) {
}

在我的 Thymeleaf 模板中有

<ul><li th:each = "Comment :${image.comments}" th:text = "${image.comments}"></li></ul>

这给了我这样的线条

[评论(id=5a623d5d2298352bc4929866,imageId=0d46b575-b6ce-48e2-988a-ebe62ebc2ceb,comment=test),评论(id=5a623d8b2298352bc4929867,commentId=0d46b575-b6ce-48ec2-98)

显示评论记录与 MongoDB 键/ID 和其他所有内容一样。这不是我想要的。 我的 Thymeleaf 模板中也有这个

<ul><li th:each = "Comment :${image.comments}" th:text = "${comment == null ? 'empty' : comment.Comment}"></li></ul>

其中显示每个评论记录的单词empty

数据库中的评论记录是这样的

{ "_id" : ObjectId("5a623d5d2298352bc4929866"), "imageId" : "0d46b575-b6ce-48e2-988a-ebe62ebc2ceb", "comment" : "test", "_class" : "learningspringboot.comments.Comment" }

数据库中的图像记录是这样的

{ "_id" : ObjectId("5a623d5d2298352bc4929866"), "imageId" : "0d46b575-b6ce-48e2-988a-ebe62ebc2ceb", "comment" : "test", "_class" : "learningspringboot.comments.Comment" }
{ "_id" : ObjectId("5a623d8b2298352bc4929867"), "imageId" : "0d46b575-b6ce-48e2-988a-ebe62ebc2ceb", "comment" : "test23", "_class" : "learningspringboot.comments.Comment" }

如何解开 cmets 记录以便只看到“comment”值而不是“_id”或“imageId”值?

【问题讨论】:

    标签: spring mongodb spring-boot thymeleaf


    【解决方案1】:

    问题是我有一个hashmap&lt;string&gt;,arraylist&lt;image&gt;。 所以我只是需要使用百里香遍历数组列表中的所有图像元素。我很确定这已经完成了,我只需要找到一个很好的例子并阅读一下。

    我可以使用以下代码

    <th:block th:each="Comment : ${image.comments}">
        <ul th:each="comment : ${Comment}">
            <li th:text="${comment.comment}"></li>
        </ul>
    </th:block>
    

    现在我可以继续前进了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 2012-01-31
      • 2011-08-11
      • 1970-01-01
      相关资源
      最近更新 更多