当我构建 Spring Boot 的 Web 项目时,我无法使用 freemarker

ge1*_*023 0 java spring tomcat freemarker spring-boot

我的请求可以进入我的Welcomecontroller的方法,但似乎该方法无法返回freemarker页面给我或者spring boot没有区分freemarker(我已将.ftl文件放入/resources/templates/)

当我输入网址http://localhost:8080/index时

我从 chrome 中得到了这个,并且在 IDEA 的控制台中没有报告任何错误:

Whitelabel 错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。

2020 年 1 月 4 日星期六 22:52:53 CST 出现意外错误(类型=未找到,状态=404)。没有可用的消息

我的代码如下:

@Controller
public class WelcomeController {

    @Value("${application.message}")
    private String message;

    @GetMapping("/index")
    public String welcome(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", this.message);
        return "welcome";
    }
}
Run Code Online (Sandbox Code Playgroud)
@SpringBootApplication
public class QuestionsiteApplication {
    public static void main(String[] args) {
        SpringApplication.run(QuestionsiteApplication.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

欢迎.ftl:

<!DOCTYPE html>
<html lang="en">
<body>
    Date: ${time?date}
    <br>
    Time: ${time?time}
    <br>
    Message: ${message}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

应用程序属性:

application.message: Hello, Andy
Run Code Online (Sandbox Code Playgroud)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.UU</groupId>
    <artifactId>questionsite</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>questionsite</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <commons-lang3-version>3.1</commons-lang3-version>
    </properties>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

M. *_*num 5

从 Spring Boot 2.2 开始,Freemarker 模板的后缀ftlh不是ftl.

这记录在发行说明中。

将您的welcome.ftl名称重命名为welcome.ftlh,它将使用默认配置。