1、配置文件

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

添加依赖

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
    suffix: .html
  profiles:
    active: dev

    如果你使用的是ftl,那么更改suffix为ftl,或者不用写,默认是ftl,如果你用html开发,那么suffix标注html

2、编写controller

@GetMapping(value = "/test2")
public String test2(ModelMap modelMap){

    modelMap.put("name","木九天");
    return "/helloworld";
}

3、创建html

    在resources/template下创建helloworld.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
${name}
</body>
</html>

4、测试

崛起于Springboot2.X之前端模版freemaker(23)

5、疑惑解惑

    5.1 :使用freemaker的时候,我们在controller返回的是一个路径,同时我们也使用了ModeMap,我们在ModeMap添加了数据,然后返回路径到具体html页面,${name}  就是我们ModeMap里面的数据,希望大家不要迷惑输出结果:木九天怎么来的!

     5.2: 返回具体页面/找对应页面的时候,千万不能使用@RestController 和@ResponseBody,因为使用它们之后返回的是一个字符串而不是一个具体页面了,谨记。

 

 

转载于:https://my.oschina.net/mdxlcj/blog/1859328

相关文章: