1.freemark簡介(摘自:http://blog.csdn.net/liaomin416100569/article/details/78349072)
在互聯網軟件內容網站中 一般首頁的訪問量大,為了提供首頁的訪問效率,一般 首頁的內容以及其中的新聞等信息都可以實現html靜態化 瀏覽器訪問時
設置瀏覽器的緩存策略和生成靜態頁面的周期一致 可以使訪問效率大大提升 同時配合cdn處理圖片 js css等資源 可以在首頁訪問時 理論完全脫離數據庫
降低應用壓力
原理圖:
2.代碼
1).pom.xml
<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>lf</groupId>
<artifactId>Springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 繼承 spring-boot-starter-paren -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心組件 -->
<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>
2).測試類:IndexController.java
package com.lf.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("index") public String index(ModelMap map){ map.put("user", "大飛"); map.put("sex", 1); List<Object> userList = new ArrayList<Object>(); userList.add("小菜"); userList.add("老禿子"); userList.add("王麻子"); map.put("userList", userList); return "index"; } }
3).FTL模板:index.ftl (springboot默認加載模板文件地址:/src/main/resources/templates)
<!DOCTYPE html > <html> <head lang="en"> <meta charset=UTF-8"> <title>Insert title here</title> </head> <body> 用戶:${user} 性別: <#if sex==1> 男 <#elseif sex==2> 女 <#else> 其他 </#if> <br/><hr/> 遍歷集合: <#list userList as user> ${user} </#list> </body> </html>
4).springboot啟動類: ApplicationMain.java
package com.lf.application; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages="com.lf") @EnableAutoConfiguration//此注釋自動載入應用程序所需的所有Bean public class ApplicationMain { public static void main(String[] args) { SpringApplication.run(ApplicationMain.class, args); } }
5).測試結果