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 ~ ~ ~寫到這裏,就寫完了,如果有幸幫助到你,請記得關注我,共同一起見證我們的成長

 

 

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