在Spring Boot 2中解开Thymeleaf中的Kotlin HashMap
我有一个Kotlin函数,它创建一个带有hashmap的模型,如下所示
@GetMapping("/") fun index(model: Model): Mono { model.addAttribute("images", imageService.findAllImages()?.flatMap { image -> Mono.just(image) .zipWith(repository.findByImageId(image?.id!!).collectList()) .map({ imageAndComments: Tuple2<Image?, MutableList> -> hashMapOf( "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模板中
这给了我这样的线路
[Comment(id = 5a623d5d2298352bc4929866,imageId = 0d46b575-b6ce-48e2-988a-ebe62ebc2ceb,comment = test),Comment(id = 5a623d8b2298352bc4929867,imageId = 0d46b575-b6ce-48e2-988a-ebe62ebc2ceb,comment = test23)]
其中显示了与MongoDB键/ ID和其他所有内容一样的注释记录。 这不是我想要的。 我也在我的Thymeleaf模板中有这个
其中显示每个评论记录的单词为空 。
数据库中的注释记录看起来像
{ "_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" }
我怎样才能打开评论记录,所以我只看到“评论”值,而不是“_id”或“imageId”值?
问题是我有一个hashmap
。 所以我只需要用thymeleaf遍历数组列表中的所有图像元素。 我很确定之前已经完成了这个工作,我只需要找一个很好的例子并做一些阅读。
我能够使用下面的代码
现在我可以继续前进。
- Android Studio 3.0 – 无法find方法’com.android.build.gradle.internal.variant.BaseVariantData.getOutputs()Ljava / util / List’
- 如何访问generics类参数中的属性/方法