Kotlin+spring+mustache @Controller 不工作(Whitelabel 错误页面)但是@RestController 工作
Kotlin+spring+mustache @Controller does not working (Whitelabel Error Page) but @RestController working
在 Kotlin 上使用 spring 有一些问题。当我设置@RestController 并转到 localhost:8080 时,我可以看到 "index"。如果我设置@Controller,我有
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Wed May 13 23:34:59 EEST 2020
There was an unexpected error (type=Not Found, status=404).
我不知道为什么它不起作用。在 java 中,类似的代码(来自教程)有效。我的代码基于 java 教程中的代码。
我的 App.kt
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
一些控制器
@Controller
class SomeController {
@GetMapping("/")
fun index(model: Model): String {
model["something"] = "asd"
return "index"
}
}
index.mustache:
<html>
<body>
<div>
its the {{something}}
</div>
</body>
</html>
和pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
提前致谢
您的 pom.xml 中没有添加模板引擎 添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
在 Kotlin 上使用 spring 有一些问题。当我设置@RestController 并转到 localhost:8080 时,我可以看到 "index"。如果我设置@Controller,我有
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Wed May 13 23:34:59 EEST 2020
There was an unexpected error (type=Not Found, status=404).
我不知道为什么它不起作用。在 java 中,类似的代码(来自教程)有效。我的代码基于 java 教程中的代码。 我的 App.kt
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
一些控制器
@Controller
class SomeController {
@GetMapping("/")
fun index(model: Model): String {
model["something"] = "asd"
return "index"
}
}
index.mustache:
<html>
<body>
<div>
its the {{something}}
</div>
</body>
</html>
和pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
提前致谢
您的 pom.xml 中没有添加模板引擎 添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
</dependency>