這段時間的工做是作一個網址導航的項目,面向用戶的就是一個首頁,因而就想到了使用freemarker這個模板引擎來對首頁靜態化。css
以前是用jsp實現,爲了不用戶每次打開頁面都查詢一次數據庫,因此使用了jsp的內置對象application,在Controller中將數據都查詢出來,html
而後放入application,最後在JSP頁面使用jstl標籤配合EL表達式 將數據遍歷出來。這樣作是從必定程度上減輕了服務器的壓力和頁面的響應速度,數據庫
可是仍然沒有靜態頁面響應快。服務器
使用Freemarker步驟:session
<!--廣告懸浮--> <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提供了不少的標籤,這裏不一一敘述。app
// 獲取搜索引擎 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();