Search code examples
javaspringspring-boottomcatfreemarker

I can not use freemarker when i am building my spring boot's web project


My request can get into my method of my Welcomecontroller,but it semms that this method can not return a freemarker page to me or spring boot didn't distinguish freemarker(I have put .ftl files into /resources/templates/)

When I input the url http://localhost:8080/index

I got this from my chrome and don't report any errors in my IDEA's console:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Jan 04 22:52:53 CST 2020 There was an unexpected error (type=Not Found, status=404). No message available

My code is as follow:

@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>

Solution

  • As of Spring Boot 2.2 the suffix for Freemarker templates is ftlh not ftl.

    This is documented in the Release Notes.

    Rename your welcome.ftl to welcome.ftlh and it will work with the default configuration.