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

标签 java spring spring-boot tomcat freemarker

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

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

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

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

2020 年中部时间 1 月 4 日星期六 22:52:53 出现意外错误(类型=未找到,状态=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";
    }
}
@SpringBootApplication
public class QuestionsiteApplication {
    public static void main(String[] args) {
        SpringApplication.run(QuestionsiteApplication.class, args);
    }

}

welcome.ftl:

<!DOCTYPE html>
<html lang="en">
<body>
    Date: ${time?date}
    <br>
    Time: ${time?time}
    <br>
    Message: ${message}
</body>
</html>

application.properties:

application.message: Hello, Andy

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>

最佳答案

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

这记录在 Release Notes 中.

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

关于java - 当我构建 Spring Boot 的 Web 项目时,我无法使用 freemarker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59591044/

相关文章:

java - Collection 类中奇怪的控制语句

java - Android环境下可以使用GPU进行数值计算(复杂矩阵乘法)吗?

java - 外键也是主键的实体的Spring Data Repository

java - 组织和使用应用 HATEOAS 的 API

java - transient 属性值异常 : object references an unsaved transient instance - save the transient instance beforeQuery flushing

java - 在与当前类同名的类上使用 Spring @Autowired

java - 我在单独的类中为字符串数组实现合并排序算法时遇到问题

java - 在 Spring application.properties 文件中声明对象列表

spring-boot - Spring Cloud Config 不解密配置服务器密码

java - 具有继承调用的构造函数中的最终变量