1. 程式人生 > >Spring Boot使用模板freemarker【從零開始學Spring Boot(轉)

Spring Boot使用模板freemarker【從零開始學Spring Boot(轉)

dep demo attach macro 使用 doctype com 地址 2016年

視頻&交流平臺

à SpringBoot網易雲課堂視頻

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平臺

http://412887952-qq-com.iteye.com/blog/2321532

【原創文章,轉載請註明出處】

103. Spring Boot Freemarker特別篇之contextPath【從零開始學Spring Boot】

最近有好久沒有更新博客了,感謝小夥伴的默默支持,不知道是誰又打賞了我一個小紅包,謝謝。

今天我們講講怎麽在Spring Boot

中使用模板引擎freemarker,先看看今天的大綱:

寫道(1) freemarker介紹;
(2) 新建spring-boot-freemarker工程;
(3) 在pom.xml引入相關依賴;
(4) 編寫啟動類;
(5) 編寫模板文件hello.ftl;
(6) 編寫訪問類HelloController;
(7) 測試;
(8) freemarker配置;
(9) freemarker常用語法;
(10) freemarker layout 布局

(1) freemarker介紹;

FreeMarker是一款模板引擎: 即一種基於模板和要改變的數據, 並用來生成輸出文本(HTML網頁、電子郵件、配置文件、源代碼等)的通用工具。

它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發產品的組件。

(2) 新建spring-boot-freeMarker工程;

我們新建一個maven工程,取名為:spring-boot-freemarker

(3) pom.xml引入相關依賴;

這裏使用freeMarker需要引入相關依賴包:spring-boot-starter-freemarker

Xml代碼 技術分享圖片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.kfit</groupId>
  5. <artifactId>spring-boot-velocity</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>spring-boot-velocity</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <!-- jdk版本號,angel在這裏使用1.8,大家修改為大家本地配置的jdk版本號即可 -->
  13. <java.version>1.8</java.version>
  14. </properties>
  15. <!--
  16. spring boot 父節點依賴,
  17. 引入這個之後相關的引入就不需要添加version配置,
  18. spring boot會自動選擇最合適的版本進行添加。
  19. -->
  20. <parent>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-parent</artifactId>
  23. <version>1.4.1.RELEASE</version><!-- 1.4.1.RELEASE , 1.3.3.RELEASE-->
  24. </parent>
  25. <dependencies>
  26. <dependency>
  27. <groupId>junit</groupId>
  28. <artifactId>junit</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. <!-- spring boot web支持:mvc,aop... -->
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36. <!-- 引入freeMarker的依賴包. -->
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-freemarker</artifactId>
  40. </dependency>
  41. </dependencies>
  42. </project>

(4) 編寫啟動類;

啟動類沒有什麽特別之處,不過多介紹,請看代碼:

Java代碼 技術分享圖片
  1. package com.kfit;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. *
  6. * @author Angel --守護天使
  7. * @version v.0.1
  8. * @date 2016年10月4日
  9. */
  10. @SpringBootApplication
  11. public class App {
  12. publicstaticvoid main(String[] args) {
  13. SpringApplication.run(App.class, args);
  14. }
  15. }

(5) 編寫模板文件hello.ftl;

編寫一個hello.ftl文件,此文件的路徑在src/main/resources/templates下,其中hello.ftl文件的內容如下:

Html代碼 技術分享圖片
  1. <html>
  2. <body>
  3. welcome ${name} to freemarker!
  4. </body>
  5. </html>

(6) 編寫訪問類HelloController;

有了模板文件之後,我們需要有個Controller控制類,能夠訪問到hello.ftl文件,這裏也很簡單,具體看如下代碼:

Java代碼 技術分享圖片
  1. package com.kfit.demo.web;
  2. import java.util.Map;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. /**
  6. * 測試velocity;
  7. * @author Angel --守護天使
  8. * @version v.0.1
  9. * @date 2016年10月4日
  10. */
  11. @Controller
  12. public class HelloController {
  13. @RequestMapping("/hello")
  14. public String hello(Map<String,Object> map){
  15. map.put("name", "[Angel -- 守護天使]");
  16. return "hello";
  17. }
  18. }

(7) 測試;

好了,到這裏,我們就可以啟動我們的程序進行測試了,訪問地址:

http://127.0.0.1:8080/hello ,如果你在瀏覽器中看到如下信息:

welcome [Angel -- 守護天使] to freemarker!

那麽說明你的demo ok 了。

(8) freemarker配置;

spring bootapplication.properties屬性文件中為freemarker提供了一些常用的配置,如下:

########################################################

###FREEMARKER (FreeMarkerAutoConfiguration)

########################################################

spring.freemarker.allow-request-override=false

spring.freemarker.cache=true

spring.freemarker.check-template-location=true

spring.freemarker.charset=UTF-8

spring.freemarker.content-type=text/html

spring.freemarker.expose-request-attributes=false

spring.freemarker.expose-session-attributes=false

spring.freemarker.expose-spring-macro-helpers=false

#spring.freemarker.prefix=

#spring.freemarker.request-context-attribute=

#spring.freemarker.settings.*=

#spring.freemarker.suffix=.ftl

#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list

#spring.freemarker.view-names= # whitelist of view names that can be resolved

(9) freemarker常用語法;

freemarker的語法並不是本節的重點,這裏還是簡單的介紹下幾個常用的if else,list

首先我們改造下HelloControllerhello方法

Java代碼 技術分享圖片
  1. @RequestMapping("/hello")
  2. public String hello(Map<String,Object> map){
  3. map.put("name", "[Angel -- 守護天使]");
  4. map.put("gender",1);//gender:性別,1:男;0:女;
  5. List<Map<String,Object>> friends =new ArrayList<Map<String,Object>>();
  6. Map<String,Object> friend = new HashMap<String,Object>();
  7. friend.put("name", "張三");
  8. friend.put("age", 20);
  9. friends.add(friend);
  10. friend = new HashMap<String,Object>();
  11. friend.put("name", "李四");
  12. friend.put("age", 22);
  13. friends.add(friend);
  14. map.put("friends", friends);
  15. return "hello";
  16. }

這裏我們返回了genderfriends的列表;

接下來我們看看怎麽在freemarker進行展示呢?

Html代碼 技術分享圖片
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <p>
  9. welcome ${name} to freemarker!
  10. </p>
  11. <p>性別:
  12. <#if gender==0>
  13. <#elseif gender==1>
  14. <#else>
  15. 保密
  16. </#if>
  17. </p>
  18. <h4>我的好友:</h4>
  19. <#list friends as item>
  20. 姓名:${item.name} , 年齡${item.age}
  21. <br>
  22. </#list>
  23. </body>
  24. </html>

(10) freemarker layout

freemarker layout主要處理具有相同內容的頁面,比如每個網站的headerfooter頁面。

freemarker 的布局主要常見的兩種方式是#import(“文件路徑”)#include(“文件路徑”),其中importinclude的區別在於,include常用於公共部分的頁面,如果要使用<#assign username=“張三”>涉及到內部函數以及變量聲明之類的,使用import進行導入,如果在import中的頁面含有頁面當前將不會進行渲染。 我們編寫一個headerfooter,其中的header使用include引入,footer頁面也使用include引入。(當然freemarker 還有別的布局方式,這裏只是介紹一種,請自行學習研究)

header.ftl內容:

Html代碼 技術分享圖片
  1. <header>
  2. This is a header,welcome ${name} to my web site!
  3. </header>
  4. <hr>

footer.ftl內容:

Html代碼 技術分享圖片
  1. <hr>
  2. <footer>
  3. This is a footer,welcome ${name} to my web site!
  4. </footer>

修改hello.ftl

Html代碼 技術分享圖片
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  4. <head>
  5. <title>Hello World!</title>
  6. </head>
  7. <body>
  8. <#include "/header.ftl" >
  9. <p>
  10. welcome ${name} to freemarker!
  11. </p>
  12. <p>性別:
  13. <#if gender==0>
  14. <#elseif gender==1>
  15. <#else>
  16. 保密
  17. </#if>
  18. </p>
  19. <h4>我的好友:</h4>
  20. <#list friends as item>
  21. 姓名:${item.name} , 年齡${item.age}
  22. <br>
  23. </#list>
  24. <#include "/footer.ftl" >
  25. </body>
  26. </html>

到這裏就ok了,我們訪問/hello頁面,應該會看到如下圖的效果:


技術分享圖片

103. Spring Boot Freemarker特別篇之contextPath【從零開始學Spring Boot】

視頻&交流平臺

à SpringBoot網易雲課堂視頻

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平臺

http://412887952-qq-com.iteye.com/blog/2321532

Spring Boot使用模板freemarker【從零開始學Spring Boot(轉)