在Springboot内使用Freemaker制作网页失败,如何解决?

问题遇到的现象和发生背景

使用FreeMaker开发Springboot项目失败,按照网上的教程进行的操作。

问题相关代码,请勿粘贴截图

如下为main.java

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class main {
    public static void main(String[] args) {
        SpringApplication.run(main.class, args);
    }
}

如下为FreemarkerIndexController.java

package org.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class FreemarkerIndexController {

    @RequestMapping("/1")
    public String freemarkerIndex(Map<String, String> result){
        result.put("name","Demo");
        return "freemakerIndex";
    }
}


如下为pom.xml


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>SpringbootDemoartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.2 version>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>

    dependencies>


    <properties>
        <maven.compiler.source>18maven.compiler.source>
        <maven.compiler.target>18maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

project>

相关文件结构:

img

运行结果及报错内容
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.2)

2022-07-28 19:00:09.185  INFO 11780 --- [           main] org.example.main                         : Starting main using Java 18.0.2 on 10_0_4_2 with PID 11780 (C:\SpringbootDemo\target\classes started by Administrator in C:\SpringbootDemo)
2022-07-28 19:00:09.189  INFO 11780 --- [           main] org.example.main                         : No active profile set, falling back to 1 default profile: "default"
2022-07-28 19:00:10.217  INFO 11780 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-07-28 19:00:10.230  INFO 11780 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-07-28 19:00:10.230  INFO 11780 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-07-28 19:00:10.326  INFO 11780 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-07-28 19:00:10.327  INFO 11780 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1076 ms
2022-07-28 19:00:10.808  INFO 11780 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-28 19:00:10.818  INFO 11780 --- [           main] org.example.main                         : Started main in 2.071 seconds (JVM running for 2.467)
2022-07-28 19:00:23.221  INFO 11780 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-07-28 19:00:23.222  INFO 11780 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-07-28 19:00:23.224  INFO 11780 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms

img

我想要达到的结果

修复问题,正常显示页面,其中页面文件位于templates目录下,内容为

html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>title>
head>
<body>
      123
body>
html>


img

检查下 application 文件是否有配置

spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
```java

参考:https://www.csdn.net/tags/NtDaYg4sNzQxOC1ibG9n.html


```

解决方法:

  1. 添加配置spring.freemarker.suffix=.ftl
  2. 将文件freemakerIndex.ftl改成freemakerIndex.ftlh