5分钟学会springboot整合Freemarker

                                              springboot整合Freemarker

一、前言

       springboot整合Freemarker。

       在此记录下,分享给大家。

 二、springboot整合Freemarker

                                                 

1、pom文件 依赖引入

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>
        <!-- SpringBoot 测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- SpringBoot 整合 Freemarker -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!-- SpringBoot web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2、 application.yml 新增配置

spring:
  http:
    encoding:
      force: true
      # 模板引擎编码为UTF-8
      charset: UTF-8
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    # 模板文件结尾.ftl
    suffix: .ftl
    # 模板文件目录
    template-loader-path: classpath:/templates

3、FreemarkerController.java

/**
 * Freemarker测试
 *      Controller
 * @author yys
 */
@Controller
public class FreemarkerController {

    @RequestMapping("/index")
    public String index(Map<String, Object> map) {
        // 花名
        map.put("name", "yys");
        // 性别
        map.put("sex", "1");
        // 爱好
        List<String> list = new ArrayList<String>(0);
        list.add("挑灯写博客");
        list.add("打羽毛球");
        list.add("健身");
        map.put("hobbys", list);
        return "/index";

    }

}

4、启动类

@SpringBootApplication
public class YysApp {

    public static void main(String[] args) {
        SpringApplication.run(YysApp.class, args);
    }

}

5、index.ftl

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>一生猿,一世猿。</title>
</head>
<style>
    .table-div table{ border:1px solid black; }
    .table-div table td{ border:1px solid black; }
</style>
<body>
    <div class="table-div">
        <table class="table">
            <thead>简介:</thead>
            <tbody>
            <tr>
                <td>花名</td>
                <td>${name}</td>
            </tr>
            <tr>
                <td>性别</td>
                <td>
                    <#if sex == "1">
                        男
                    <#elseif sex == "2">
                        女
                    <#else>
                        other
                    </#if>
                </td>
            </tr>
            <tr>
                <td>爱好</td>
                <td>
                    <#list hobbys as hobby>
                        ${hobby}&nbsp;
                    </#list>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

6、测试

http://localhost:8080/index

  a、页面结果 - 如下图所示 :

                                         

                       Now ~ ~ ~写到这里,就写完了,如果有幸帮助到你,请记得关注我,共同一起见证我们的成长

发布了74 篇原创文章 · 获赞 253 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_42175986/article/details/103266884