視頻&交流平台:
http://study.163.com/course/introduction.htm?courseId=1004329008
http://412887952-qq-com.iteye.com/blog/2321532
【原創文章,轉載請注明出處】
103. Spring Boot Freemarker特別篇之contextPath【從零開始學Spring Boot】
最近有好久沒有更新博客了,感謝小伙伴的默默支持,不知道是誰又打賞了我一個小紅包,謝謝。
今天我們講講怎么在Spring Boot中使用模板引擎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,
- <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.kfit</groupId>
- <artifactId>spring-boot-velocity</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>spring-boot-velocity</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <!-- jdk版本號,angel在這里使用1.8,大家修改為大家本地配置的jdk版本號即可 -->
- <java.version>1.8</java.version>
- </properties>
- <!--
- spring boot 父節點依賴,
- 引入這個之后相關的引入就不需要添加version配置,
- spring boot會自動選擇最合適的版本進行添加。
- -->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.4.1.RELEASE</version><!-- 1.4.1.RELEASE , 1.3.3.RELEASE-->
- </parent>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- spring boot web支持:mvc,aop... -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- 引入freeMarker的依賴包. -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-freemarker</artifactId>
- </dependency>
- </dependencies>
- </project>
(4) 編寫啟動類;
啟動類沒有什么特別之處,不過多介紹,請看代碼:
- package com.kfit;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- /**
- *
- * @author Angel --守護天使
- * @version v.0.1
- * @date 2016年10月4日
- */
- @SpringBootApplication
- public class App {
- publicstaticvoid main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- }
- @author Angel --守護天使
- @version v.0.1
- @date 2016年10月4日
*/
@SpringBootApplication
public class App {
publicstaticvoid main(String[] args) {
SpringApplication.run(App.class, args);
}
}
(5) 編寫模板文件hello.ftl;
編寫一個hello.ftl文件,此文件的路徑在src/main/resources/templates下,其中hello.ftl文件的內容如下:
- <html>
- <body>
- welcome ${name} to freemarker!
- </body>
- </html>
(6) 編寫訪問類HelloController;
有了模板文件之后,我們需要有個Controller控制類,能夠訪問到hello.ftl文件,這里也很簡單,具體看如下代碼:
- package com.kfit.demo.web;
- import java.util.Map;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- /**
- * 測試velocity;
- * @author Angel --守護天使
- * @version v.0.1
- * @date 2016年10月4日
- */
- @Controller
- public class HelloController {
- @RequestMapping("/hello")
- public String hello(Map<String,Object> map){
- map.put("name", "[Angel -- 守護天使]");
- return "hello";
- }
- }
-
測試velocity;
-
@author Angel --守護天使
-
@version v.0.1
-
@date 2016年10月4日
*/
@Controller
public class HelloController {@RequestMapping("/hello")
public String hello(Map<String,Object> map){
map.put("name", "[Angel -- 守護天使]");
return "hello";
}
}
(7) 測試;
好了,到這里,我們就可以啟動我們的程序進行測試了,訪問地址:
http://127.0.0.1:8080/hello ,如果你在瀏覽器中看到如下信息:
welcome [Angel -- 守護天使] to freemarker!
那么說明你的demo ok 了。
(8) freemarker配置;
在spring boot的application.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;
首先我們改造下HelloController的hello方法
- @RequestMapping("/hello")
- public String hello(Map<String,Object> map){
- map.put("name", "[Angel -- 守護天使]");
- map.put("gender",1);//gender:性別,1:男;0:女;
- List<Map<String,Object>> friends =new ArrayList<Map<String,Object>>();
- Map<String,Object> friend = new HashMap<String,Object>();
- friend.put("name", "張三");
- friend.put("age", 20);
- friends.add(friend);
- friend = new HashMap<String,Object>();
- friend.put("name", "李四");
- friend.put("age", 22);
- friends.add(friend);
- map.put("friends", friends);
- return "hello";
- }