问题描述
我正在穿过 tutorial on Spring Boot using Kotlin。我是 Kotlin 和 Spring 的新手,尽管我已经在其他语言的几个不同的 MVC 风格框架中工作过,所以请了解要点。
package com.example.blog
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.ui.set
import org.springframework.web.bind.annotation.GetMapping
@Controller
class HtmlController(private val repository: ArticleRepository) {
@GetMapping("/")
fun blog(model: Model): String {
model["title"] = "Blog"
model["articles"] = repository.findAllByOrderByAddedAtDesc().map {
it.render()
}
return "blog"
}
}
我们在最后看到 return "blog"
的地方 Spring 知道使用提供的模型返回一个 mustache 模板。
我会期望像 return MustacheView("blog",model)
之类的东西(我知道这不是一个真正的类或方法,只是用来举例说明的伪代码)或类似的东西,您明确说“嘿,将它作为胡子模板返回,而不是只是字符串 blog
"..
我确信在幕后有一些魔法,spring 说“哦,嘿,你安装了 mustache 依赖项,当你从控制器返回这个字符串时,你必须指的是模板名称”,但我不能请参阅文档中详细说明的位置。
完成本教程并不完全有必要知道,但目前感觉有点莫名其妙,我想知道发生了什么。
解决方法
当您返回 Future<void> getData() async {
await this.getCoordinates();
this.addMarker(LatLng(coordinatesStart!.latitude!,coordinatesStart!.longitude!),'origin',BitmapDescriptor.defaultMarker);
await this.getPolyLines();
notifyListeners();
}
时,它被用作视图名称。此视图名称为 resolved into a View
implementation。
在 Mustache 的情况下,Spring Boot 会自动配置一个 MustacheViewResolver
bean。这个 bean 然后被 Spring MVC 选取并用于将您的 String
转换为由您的 Mustache 模板支持的 "blog"
。然后将它与传递到控制器方法并呈现的 MustacheView
结合起来。