這段時間的工作是做一個網址導航的項目,面向用戶的就是一個首頁,於是就想到了使用freemarker這個模板引擎來對首頁靜態化。
之前是用jsp實現,為了避免用戶每次打開頁面都查詢一次數據庫,所以使用了jsp的內置對象application,在Controller中將數據都查詢出來,
然后放入application,最后在JSP頁面使用jstl標簽配合EL表達式 將數據遍歷出來。這樣做是從一定程度上減輕了服務器的壓力和頁面的響應速度,
但是仍然沒有靜態頁面響應快。
使用Freemarker步驟:
- jar包,我的項目中使用maven來構建,所以在pom.xml中引入Freemarker jar包的坐標就可以了。
- ftl模板,我在WEB-INF下面創建一個文件夾ftl,里面只放ftl模板文件,我創建了一個index.ftl文件。
- ftl模板文件中寫的就是html標簽和css樣式之類的,但是數據部分需要使用Freemarker提供的標簽遍歷出來。如下
<!--廣告懸浮--> <div class="subMenu"> <!--工具--> <div class='xff'> <div class="slideTxtBox"> <div class="hd"> <span class="arrow"><a class="next"></a><a class="prev"></a></span> <ul> <#list newsMap?keys as testKey> <li>${testKey}</li> </#list> </ul> </div> <div class="bd" style="padding: 5px 10px;"> <#list newsMap?values as value> <div style="text-align: left; table-layout: fixed; word-wrap: break-word; width: 100%;" class="baidu"> <#list value as newsList> <a target="_blank" href="${newsList.newsurl }" title="${newsList.newsname }">${newsList.newsname }</a> </#list> </div> </#list> </div> </div> </div> </div>
其中<#list></#list>是Freemarker提供的遍歷標簽,Freemarker提供了很多的標簽,這里不一一敘述。
- Contorller中將數據都查詢出來,通過ftl模板取出數據,最后將完整的數據寫入html
// 獲取搜索引擎 List<SearchEngines> searchEngines = this.indexService.findSearchEngines(); // 獲取熱搜客戶 List<Catalog> hotSearchs = this.indexService.findHotSearchs(); // 獲取前25個一級目錄 CatalogCustom custom = new CatalogCustom(); custom.setCatalogLevel(1); List<Catalog> topLevelCatalog = this.indexService.findCustomers(custom); // 獲取一級目錄下的前十個客戶 Map<String, List<Catalog>> customerMap = new HashMap<String, List<Catalog>>(); for (Catalog catalog : topLevelCatalog) { CatalogCustom customer = new CatalogCustom(); customer.setCatalogLevel(3); customer.setGfid(catalog.getCatalogId()); List<Catalog> customerList = this.indexService.findCustomers(customer); customerMap.put(catalog.getCatalogName(), customerList); } // 獲取新聞相關數據 Map<String, List<News>> newsMap = new HashMap<String, List<News>>(); List<NewsCatalog> newsCatalogs = this.indexService.findNewsCatalog(); for (NewsCatalog newsCatalog : newsCatalogs) { News news = new News(); news.setPid(newsCatalog.getId()); List<News> newsList = this.indexService.findNews(news); newsMap.put(newsCatalog.getNewscatalog(), newsList); } // 獲取關鍵詞 List<Keywords> keywords = this.indexService.findKeywords(); /* application.setAttribute("newsMap", newsMap); application.setAttribute("searchEngines", searchEngines); application.setAttribute("hotSearchs", hotSearchs); application.setAttribute("customerMap", customerMap); application.setAttribute("keywords", keywords); */ String ftlPath = session.getServletContext().getRealPath("/WEB-INF/ftl"); Configuration configuration = new Configuration(); configuration.setDirectoryForTemplateLoading(new File(ftlPath)); configuration.setDefaultEncoding("UTF-8"); // 獲取或創建一個模版。 Template template = configuration.getTemplate("index.ftl"); // 獲取html靜態頁面文件 String indexPath = session.getServletContext().getRealPath("/index.html"); //設置文件輸入流編碼,不然生成的html文件會中文亂碼 FileWriterWithEncoding out = new FileWriterWithEncoding(indexPath,"UTF-8"); // 將頁面中要展示的數據放入一個map中 HashMap<String,Object> map = new HashMap<String, Object>(); map.put("newsMap", newsMap); map.put("searchEngines", searchEngines); map.put("hotSearchs", hotSearchs); map.put("customerMap", customerMap); map.put("keywords", keywords); //將map中的數據輸入到index.ftl這個模板文件中並遍歷出來,最后再將整個模板的數據寫入到index.html中。 template.process(map, out); out.close();