springboot freemarker笔记

1、首先添加maven引用

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

2、然后在配置文件添加相关配置信息

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: .ftl
    template-loader-path: classpath:/templates/

3、然后添加一个控制器

package com.example.simpledisplay.controller;

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

import java.util.Map;

@Controller
public class ViewController {

    @RequestMapping("/view/index")
    public String index(Map<String, Object> map) {
        map.put("hello", "Hello FreeMarker");
        return "index";
    }
}

4、添加视图信息,返回的视图名称要和返回字符串相同

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

注意,如果后缀名用html,会出现不识别部分标签问题,所以还是使用ftl后缀吧

 

很多时候我们需要模板嵌套,比如一个后台管理页面,菜单栏一般都是固定不变的,所以需要用到include,标签,index.ftl页面中的东西都会出现在当前页。

<#include "/index.ftl">

还有一个是import标签,来使用模板中定义的宏,就是一些自定义标签

<#import "/index.ftl" as haha>

<@haha.branchService >hhhh</@haha.branchService> 使用这个标签来使用。

 

注意:如果在父模板中有后台变量,那么当前模板引用父模板后,后台需要传值给父模板的变量

 

参考:https://blog.csdn.net/xieguojun2013/article/details/17529987

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章