一、pom.xml
須要這些依賴html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
二、ymljava
我喜歡 yml,因此刪掉 application.properties
,新建 application.yml
git
三、配置github
在 application.yml
中添加以下配置web
# freemarker spring: freemarker: template-loader-path: classpath:/templates/ cache: false charset: UTF-8 check-template-location: true content-type: text/html expose-request-attributes: true expose-session-attributes: true request-context-attribute: request suffix: .html
四、Controller - Viewspring
package com.fengwenyi.demo.freemarker.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * @author Wenyi Feng */ @Controller public class HomeController { @RequestMapping("/") public ModelAndView home() { ModelAndView mv = new ModelAndView("home"); return mv; } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>freemarker</title> </head> <body> <center> <h1>Hello FreeMarker!</h1> <h6>Welcome to Learn FreeMarker With Me!</h6> <div><i>目錄</i></div> <div> <ul> <li><a target="_blank" href="/value/get">取值 - 2018.06.13</a> </li> <li><a target="_blank" href="/value/get">運算,集合 - 2018.06.14</a> </li> <li><a target="_blank" href="/value/get">高級技巧 - 2018.06.15</a> </li> </ul> </div> </center> </body> </html>
五、運行效果sql
一、學過 freemarker 的的童鞋都知道,須要在spring的配置文件中添加上一些屬性,那Spring boot 應該怎麼作呢?session
# freemarker spring: freemarker: template-loader-path: classpath:/templates/ cache: false charset: UTF-8 check-template-location: true content-type: text/html expose-request-attributes: true expose-session-attributes: true request-context-attribute: request suffix: .html settings: #number_format: '0.##' #數字格式化,無小數點,若是有小數,只保留兩位小數 number_format: '0.#############################################' date_format: 'yyyy_MM_dd HH:mm:ss' # 這個是對java.sql.Date起做用 # boolean_format: 'Y, N' # 通常不這麼配置,由於咱們須要邏輯值,若是須要,咱們能夠在須要的地方將Boolean->String
二、Controllerapp
@GetMapping("/get") public ModelAndView getValue() { ModelAndView mv = new ModelAndView("get-value"); mv.addObject("intVar", 100); mv.addObject("longVar", 100000000000000L); mv.addObject("stringVar", "我是字符串"); mv.addObject("doubleVar", Math.PI); // mv.addObject("doubleVar", 3.14); // mv.addObject("doubleVar", 3.1415D); mv.addObject("booleanVar", Boolean.TRUE); mv.addObject("dateUtilVar", new Date()); mv.addObject("dateSqlVar", new java.sql.Date(new Date().getTime())); mv.addObject("nullVar", null); return mv; }
三、Viewspring-boot
<table border="1"> <tr> <th width="200">Type</th> <th width="300">Value</th> </tr> <tr> <td align="center">Integer</td> <td align="center"><font color="red">${intVar}</font></td> </tr> <tr> <td align="center">Long</td> <td align="center"><font color="red">${longVar}</font></td> </tr> <tr> <td align="center">String</td> <td align="center"><font color="red">${stringVar}</font></td> </tr> <tr> <td align="center">Double</td> <td align="center"><font color="red">${doubleVar}</font></td> </tr> <tr> <td align="center">Boolean</td> <td align="center"><font color="red">${booleanVar?string('Yes', 'No')}</font></td> </tr> <tr> <td align="center">java.util.Date</td> <td align="center"><font color="red">${dateUtilVar?string('yyyy/MM/dd HH:mm:ss')}</font></td> </tr> <tr> <td align="center">java.sql.Date</td> <td align="center"><font color="red">${dateSqlVar}</font></td> </tr> <tr> <td align="center">null</td> <td align="center"><font color="red">${nullVar!}</font></td> </tr> <tr> <td align="center">null</td> <td align="center"><font color="red">${nullVar! '-'}</font></td> </tr> <tr> <td align="center">不存在的變量</td> <td align="center"><font color="red">${notExist! '-'}</font></td> </tr> </table>
四、效果
一、FreeMarker 模塊的代碼已上傳至Github: https://github.com/fengwenyi/FreeMarker-demo
二、學習視頻:Java模板引擎之Freemarker