Spring Boot教程
作者: 时海
集成freemarker

1、maven依赖

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

2、配置文件

spring:
  freemarker:
    enabled: true
    chaset: UTF-8
    suffix: .ftl
    content-type: text/html; charset=utf-8
    template-loader-path: classpath:/templates
    cache: false
  mvc:
    static-path-pattern: classpath:/static/**

3、控制器

package com.example.boot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class IndexController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("name", "zhansan");
        return "index";
    }
}

4、ftl模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>FreeMarker</title>
</head>
<body>
<h1 >hello: ${name}</h1>
</body>
</html>




一个创业中的苦逼程序员
  • 回复
隐藏