8、SpringBoot 使用 freemarker

SpringBoot 使用 freemarker

pom.xml 中引入 freemarker

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

关闭 freemarker 缓存

spring:
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: utf-8
    content-type: text/html
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false

编写模板文件 hello.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>
        Hello freemarker !
        <br>
        My name is <span>${name}</span>!
    </h1>
</body>
</html>

编写访问文件的 controller

package cn.ylx.controller;

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

import java.util.Map;

@Controller
@RequestMapping("/templates")
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(Map<String,Object> map){
        map.put("name", "Nancy");
        return "hello";
    }

}

测试:能成功取值。

猜你喜欢

转载自blog.csdn.net/weixin_42112635/article/details/84722302