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

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