Spring Boot使用模板freemarker的示例代碼
最近有好久沒有更新博客了,感謝小伙伴的默默支持,不知道是誰又打賞了我一個小紅包,謝謝。
今天我們講講怎么在Spring Boot中使用模板引擎freemarker,先看看今天的大綱:
(1) freemarker介紹;
(2) 新建spring-boot-freemarker工程;
(3) 在pom.xml引入相關(guān)依賴;
(4) 編寫啟動類;
(5) 編寫模板文件hello.ftl;
(6) 編寫訪問類HelloController;
(7) 測試;
(8) freemarker配置;
(9) freemarker常用語法;
(10) freemarker layout 布局
(1) freemarker介紹;
FreeMarker是一款模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本(HTML網(wǎng)頁、電子郵件、配置文件、源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發(fā)產(chǎn)品的組件。
(2) 新建spring-boot-freeMarker工程;
我們新建一個maven工程,取名為:spring-boot-freemarker
(3) 在pom.xml引入相關(guān)依賴;
這里使用freeMarker需要引入相關(guān)依賴包: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 父節(jié)點依賴, 引入這個之后相關(guān)的引入就不需要添加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); } }
(5) 編寫模板文件hello.ftl;
編寫一個hello.ftl文件,此文件的路徑在src/main/resources/templates下,其中hello.ftl文件的內(nèi)容如下:
<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"; } }
(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的語法并不是本節(jié)的重點,這里還是簡單的介紹下幾個常用的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"; }
這里我們返回了gender和friends的列表;
接下來我們看看怎么在freemarker進行展示呢?
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <p> welcome ${name} to freemarker! </p> <p>性別: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密 </#if> </p> <h4>我的好友:</h4> <#list friends as item> 姓名:${item.name} , 年齡${item.age} <br> </#list> </body> </html>
(10) freemarker layout
freemarker layout主要處理具有相同內(nèi)容的頁面,比如每個網(wǎng)站的header和footer頁面。
freemarker 的布局主要常見的兩種方式是#import(“文件路徑”)和#include(“文件路徑”),其中import和include的區(qū)別在于,include常用于公共部分的頁面,如果要使用<#assign username=“張三”>涉及到內(nèi)部函數(shù)以及變量聲明之類的,使用import進行導入,如果在import中的頁面含有頁面當前將不會進行渲染。 我們編寫一個header和footer,其中的header使用include引入,footer頁面也使用include引入。(當然freemarker 還有別的布局方式,這里只是介紹一種,請自行學習研究)
header.ftl內(nèi)容:
<header> This is a header,welcome ${name} to my web site! </header> <hr>
footer.ftl內(nèi)容:
<hr> <footer> This is a footer,welcome ${name} to my web site! </footer>
修改hello.ftl:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <#include "/header.ftl" > <p> welcome ${name} to freemarker! </p> <p>性別: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密 </#if> </p> <h4>我的好友:</h4> <#list friends as item> 姓名:${item.name} , 年齡${item.age} <br> </#list> <#include "/footer.ftl" > </body> </html>
到這里就ok了,我們訪問/hello頁面,應(yīng)該會看到如下圖的效果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Springboot整合freemarker 404問題解決方案
- 基于Freemarker和xml實現(xiàn)Java導出word
- SpringBoot2.2.X用Freemarker出現(xiàn)404的解決
- SpringBoot使用FreeMarker模板發(fā)送郵件
- SpringBoot整合freemarker的講解
- spring boot 集成 shiro 自定義密碼驗證 自定義freemarker標簽根據(jù)權(quán)限渲染不同頁面(推薦
- spring boot里增加表單驗證hibernate-validator并在freemarker模板里顯示錯誤信息(推薦)
- 詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架
- 新手入門學習Spring Freemarker教程解析
相關(guān)文章
使用JPA中@Query 注解實現(xiàn)update 操作方法(必看)
下面小編就為大家?guī)硪黄褂肑PA中@Query 注解實現(xiàn)update 操作方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06HttpClient的DnsResolver自定義DNS解析另一種選擇深入研究
這篇文章主要為大家介紹了HttpClient的DnsResolver自定義DNS解析另一種選擇深入研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10解決SpringBoot集成Eureka導致返回結(jié)果由json變?yōu)閤ml的問題
這篇文章主要介紹了解決SpringBoot集成Eureka導致返回結(jié)果由json變?yōu)閤ml的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07使用springboot 獲取控制器參數(shù)的幾種方法小結(jié)
這篇文章主要介紹了使用springboot 獲取控制器參數(shù)的幾種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12