SpringBoot系列教程web篇之Freemaker環境搭建
現在的開發現狀比較流行前後端分離,使用springboot搭建一個提供rest介面的後端服務特別簡單,引入spring-boot-starter-web
依賴即可。那麼在不分離的場景下,比如要開發一個後端使用的控制檯,這時候可能並沒有前端資源,由javaer自己來客串一把,我希望簡單一點,前後端專案都整合在一起,一個jar包執行起來就完事,可以怎麼搞呢?
本篇將介紹一下如何使用springboot集合freemaker引擎來搭建web應用
I. 準備
Freemaker是模板引擎,和jsp的作用差不多,對於它的不太清楚的同學可以參考一下官方檔案 freemarker.apache.org/docs/index.…
1. 依賴
首先我們是需要一個springboot專案,基本的pom結構大都相似
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from update -->
</parent >
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<java.version>1.8</java.version >
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
複製程式碼
在這個專案中,我們主要需要引入兩個依賴包,一個web,一個freemaker
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
複製程式碼
2. 配置引數
通常我們直接使用預設的freemaker引數配置即可,下面給出幾個常用的
spring:
freemarker:
charset: UTF-8
# 本機測試時建議設定為false,上線時設定為true
cache: true
# 表示模板檔案(類html檔案)的字尾
suffix: .ftl
複製程式碼
freemaker的引數,主要對應的是org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties
II. 專案搭建演示
1. 專案結構
搭建一個web專案和我們之前的純後端專案有點不一樣,前端資源放在什麼地方,依賴檔案怎麼處理都是有講究的,下面是一個常規的專案結構
如上圖,前端資原始檔預設放在resources目錄下,下面有兩個目錄
-
templates
:存放模板檔案,可以理解為我們編寫的html,注意這個檔名不能有問題 -
static
: 存放靜態資原始檔,如js,css,image等
2. Rest服務
我們這裡提供了三個介面,主要是為了演示三種不同的資料繫結方式
@Controller
public class IndexController {
@GetMapping(path = {"","/","/index"})
public ModelAndView index() {
Map<String,Object> data = new HashMap<>(2);
data.put("name","YiHui Freemarker");
data.put("now",LocalDateTime.now().toString());
return new ModelAndView("index",data);
}
/**
* 一般不建議直接使用jdk的String.split來分割字串,內部實現是根據正則來處理的,雖然更強大,但在簡單的場景下,效能開銷更大
*/
private static String[] contents =
("綠蟻浮觴香泛泛,黃花共薦芳辰。\n清霜天宇淨無塵。\n登高宜有賦,拈筆戲成文。\n可奈園林搖落盡,悲秋意與誰論。\n眼中相識幾番新。\n龍山高會處,落帽定何人。").split("\n");
private static Random random = new Random();
@GetMapping(path = "show1")
public String showOne(Model model) {
model.addAttribute("title","臨江仙");
model.addAttribute("content",contents[random.nextInt(6)]);
return "show1";
}
@GetMapping(path = "show2")
public String showTow(Map<String,Object> data) {
data.put("name","Show2---->");
data.put("now",LocalDateTime.now().toString());
return "show2";
}
}
複製程式碼
上面的三種case中
- 第一個是最好理解的,在建立
ModelAndView
時,傳入viewName和資料 - 第二個是通過介面引數Model,設定傳遞給view的資料
- 第三種則直接使用Map來傳遞資料
三個介面,對應的三個html檔案,如下
index.ftl
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="SpringBoot FreeMaker"/>
<meta name="author" content="YiHui"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
<link rel="stylesheet" href="index.css"/>
</head>
<body>
<div>
<div class="title">hello world!</div>
<br/>
<div class="content">歡迎訪問 ${name}</div>
<br/>
<div class="sign">當前時間: ${now}</div>
<br/>
<a href="show1">傳參2測試</a>
<a href="show2">傳參3測試</a>
</div>
</body>
</html>
複製程式碼
show1.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="SpringBoot thymeleaf"/>
<meta name="author" content="YiHui"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
<link rel="stylesheet" href="index.css"/>
</head>
<body>
<div>
<div class="title">${title}</div>
<div class="content">${content}</div>
</div>
</body>
</html>
複製程式碼
show2.ft
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<title>YiHui's SpringBoot Demo</title>
<link rel="stylesheet" href="index.css"/>
</head>
<body>
<div>
<div class="title">${name}</div>
<div class="content">${now}</div>
</div>
</body>
</html>
複製程式碼
在上面的模板檔案中,需要注意引用css樣式檔案,路徑前面並沒有static,我們對應的css檔案
index.css
.title {
color: #c00;
font-weight: normal;
font-size: 2em;
}
.content {
color: darkblue;
font-size: 1.2em;
}
.sign {
color: lightgray;
font-size: 0.8em;
font-style: italic;
}
複製程式碼
3. 演示
啟動專案後,可以看到三個頁面的切換,模板中的資料根據後端的返回替換,特別是主頁的時間,每次重新整理都會隨之改變
II. 其他
0. 專案&系列文章
-
專案地址: github.com/liuyueyi/sp…
1. 一灰灰Blog
盡信書則不如,以上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
下面一灰灰的個人部落格,記錄所有學習和工作中的博文,歡迎大家前去逛逛
- 一灰灰Blog個人部落格 blog.hhui.top
- 一灰灰Blog-Spring專題部落格 spring.hhui.top