【问题标题】:Kotlin+spring+mustache @Controller does not working (Whitelabel Error Page) but @RestController workingKotlin+spring+mustache @Controller 不工作(Whitelabel 错误页面)但 @RestController 工作
【发布时间】:2020-05-13 21:01:14
【问题描述】:

在 Kotlin 上使用 spring 有一些问题。当我设置 @RestController 并转到 localhost:8080 时,我可以看到“索引”。如果我设置@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 教程中的代码。 我的应用.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>

提前致谢

【问题讨论】:

  • 为什么你认为它应该有效?您的视图在哪里配置?
  • 嗯...有人制作了教程,它可以工作。所以我想它应该也可以工作

标签: java spring spring-boot kotlin mustache


【解决方案1】:

你的 pom.xml 中没有添加模板引擎添加这个:

<dependency>          
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mustache</artifactId>
</dependency>

【讨论】:

  • 是的。我开始在 Java 上编写它,然后尝试在 Kotlin 上移动它并添加所有依赖项,它就可以工作了。对不起,愚蠢的问题)。无论如何谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2018-11-14
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多