整合FreeMarker

7/6/2023

# 导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
1
2
3
4

# 修改配置文件

spring:
  resources:
    static-locations:
      - classpath:/static/     #指定静态资源的存放路径, 可以直接进行访问
  freemarker:
    suffix: .ftl
    template-loader-path:
      - classpath:/templates/     #指定ftl模板文件的存放路径
1
2
3
4
5
6
7
8

# 添加模板内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>login success</h1>
<p>${txt}</p>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11

# 代码编写

@Controller
public class FreemarkerController {

    @GetMapping("/dsa")
    public String asd(Model model) {
        model.addAttribute("txt", "niupi");
        return "asd";
    }
}
1
2
3
4
5
6
7
8
9