我遵循了此代码示例https://kotlinlang.org/docs/tutorials/spring-boot-restful.html,以使基本的Spring Boot应用程序正常工作。 现在,我想通过thymleaf添加前端组件。在输出的html中,将变量传递给Model dont始终为null
控制器
@Controller
class IndexController {
@GetMapping("/")
fun index(@RequestParam(value = "name", defaultValue = "brian") name: String, model: Model): String {
model.addAttribute("name", name)
model.addAttribute("title", "some title")
return "index"
}
}
resources / templates / index.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${title}"></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div class="page-content">
<h1 th:text="|Hello ${name}!|"></h1>
<h2 th:text="|Welcome to ${title} application|"></h2>
</div>
</body>
</html>
春天似乎掩盖了很多事情,所以也许我错过了一些显而易见的事情。
和构建gradle
plugins {
id("org.springframework.boot") version "2.1.6.RELEASE"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.2.71"
kotlin("plugin.spring") version "1.2.71"
}
group = "com.ghost"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
//extra["springCloudVersion"] = "Greenwich.SR1"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// implementation("org.springframework.cloud:spring-cloud-gcp-starter")
// implementation("org.springframework.cloud:spring-cloud-gcp-starter-storage")
// implementation("org.springframework.cloud:spring-cloud-starter-sleuth")
implementation("org.springframework.session:spring-session-core")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
//dependencyManagement {
// imports {
// mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
// }
//}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}