Springboot 內建 Freemarker
網址
Freemarker 官網:
https://freemarker.apache.org/Freemarker 手冊網址:
http://freemarker.foofun.cn/spring-boot-starter-freemarker maven位址:
http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarkerFreemarker maven位址:
http://mvnrepository.com/artifact/org.freemarker/freemarker內建
在 pom.xml 添加依賴
<!--springboot預設支援freemarker,無需添加版本号-->
<!-- freemarker start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- freemarker end -->
項目
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>top.simba1949</groupId>
<artifactId>Springboot-Freemarker</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 繼承spring-boot-start-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<!--配置管理-->
<properties>
<!--配置項目編碼-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--jdk編譯版本-->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--依賴管理-->
<dependencies>
<!--springboot-web start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot-web end-->
<!--springboot test start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springboot test end-->
<!-- freemarker start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- freemarker end -->
</dependencies>
</project>
templates 目錄
在 src/resources目錄下建立 templates 檔案夾,用于放置 freemarker 模闆檔案
user.ftl 檔案
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<#-- Greet the user with his/her name -->
<h1>Welcome</h1>
<p>We have these animals:
<ul>
${user}
</ul>
</body>
</html>
java 代碼
啟動類 App.java
package top.simba1949;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author [email protected]
* @date 2018/7/13 19:38
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
UserController.java
package top.simba1949.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author [email protected]
* @date 2018/7/13 19:39
* a.這裡不是走 HTTP + JSON 模式,使用了 @Controller 而不是先前的 @RestController
* b.方法傳回值是 String 類型,和 application.properties 配置的 Freemarker 檔案配置路徑下的各個 *.ftl 檔案名一緻。這樣才會準确地把資料渲染到 ftl 檔案裡面進行展示。
* c.用 Model 類,向 Model 加入資料,并指定在該資料在 Freemarker 取值指定的名稱。
*/
@RequestMapping("/user")
@Controller
public class UserController {
@GetMapping
public String demo(Model model){
model.addAttribute("user","springboot內建freemarker");
// 在 templates 目錄下多了一層目錄 user
// return "user/user"; // 倆個return都可以
return "/user/user";
}
}
通路測試
http://localhost:8888/user