- 添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<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>
- 編寫配置文件(application.yml)
spring:
freemarker:
cache: false
為了實現熱部署,這里僅配置一下freemarker的緩存,關於freemarker的其它配置使用默認即可。
- 創建freemarker模板
在src/java/resources目錄下創建templates文件夾並創建demo.ftl。
模板默認是從【classpath:/templates/】這個位置查找的。
demo.ftl文件內容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
請看說明:${descrip} <br />
haahaaaa
</body>
</html>
- 創建web層輔助類
@Controller
public class FreemarkerController {
@RequestMapping("/demo")
public String demo(Map<String, Object> map) {
map.put("descrip", "It's a springboot integrate freemarker's demo!!!!");
return "demo";
}
}
- 創建測試啟動類
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
- 測試
在瀏覽器中輸入http://localhost:8080/demo
結果如下:
源代碼鏈接:https://github.com/myNameIssls/springboot-study
參考鏈接:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
關於springboot配置文件中的屬性及freemarker的配置可參考鏈接:
http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#appendix