spring boot2.1.1整合freemarker

环境

idea 2019
jdk1.8
maven3.6
spring boot2.1.1(目前springboot最高版本为2.1.8情况下,2.0以上版本都是可行的,2.0以下未测试)
项目类型:jar

1 导入依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.1.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.wn.demogroupId>
    <artifactId>spring-boot-freemarkerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-boot-freemarkername>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

2 编写application.properties/yml文件

spring:
  freemarker:
    cache: false # 浏览器缓存,开发阶段应设为false,应为经常会修改
    charset: utf-8 # 字符集
    suffix: .html # 模板后缀名,不设置的情况下默认为ftl
    template-loader-path: classpath:/templates/ # 模板加载路径

3 编写Controller类和模板html

在spring boot核心类所在包及其子包下创建Controller类

package com.wn.demo.springboot.controller;


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

import java.util.HashMap;
import java.util.Map;

@Controller
public class StudentController {

    @GetMapping("/demo")
    public String studentDemo(Model model) {
        // 构造数据
        Map<String, Object> studnet = new HashMap<>();
        studnet.put("id", 1001);
        studnet.put("name", "曹操");
        studnet.put("sex", true);
        // 将数据加入Model中
        model.addAttribute("student", studnet);
        // 返回视图名称 prefix:classpath:/templates/  suffix:.html
        return "demo";
    }
}

在src/main/resources/templates/目录下创建模板demo.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<table class="">
    <tr>
        <td>学号td>
        <td>姓名td>
        <td>性别td>
    tr>
    <tr class="text-info">
        <td>${student.id}td>
        <td>${student.name}td>
        <td>${student.sex?then('男', '女')}td>
    tr>
table>
body>
html>

总结

因为spring boot官方支持freemarker,所以spring boot整合freemarker很方便,这里只做简单记录。

你可能感兴趣的:(spring boot2.1.1整合freemarker)