Freemarker生成HTML静态页面
这段时间的工作是做一个网址导航的项目,面向用户的就是一个首页,于是就想到了使用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();
Freemarker生成HTML静态页面的更多相关文章
- 网页静态化解决方案:Freemarker生成简单html页面
FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不仅 ...
- asp .net 模板引擎 使用 Razor 生成html静态页面
刚开始不是理解 写完之后 觉得还蛮简单的 分为这几个步骤 1.获取页面模板Html 2.获取数据 3.解析模板和数据,生成静态页Html代码 4.生成静态文件 模板形式是mvc的模式,会mvc 看一下 ...
- PHP生成HTML静态页面。
function Generate(){ $html = '<!DOCTYPE html><html lang="en"><head> < ...
- [freemarker篇]02.生成HTML的静态页面
昨天完成了一部分的今天在上次的基础上,完成完成生成HTML静态页面的操作,中间会涉及一点标签的简单使用.今天的代码有一丢丢的对付的感觉!抱歉了,直接就上代码吧!求原谅! 项目结构目录如下: 第一步,新 ...
- 浅谈php生成静态页面
一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...
- 用 Smarty 生成静态页面入门介绍
why Smarty? 随着公司首页(以下简称首页)流量越来越大,最近开始考虑使用后台语言生成静态页面的技术. 我们知道,一个简单页面一般是一个 .html(或者 .htm ..shtml)后缀的文件 ...
- 比较详细PHP生成静态页面教程
一,PHP脚本与动态页面. PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合, 也可以类,函数封装等形式,以模板的方式对用户请求进行处理.无论以何种方式,它的基本原理是这样的.由客 ...
- 三种C#.net生成静态页面的方法
ASP.NET生成静态页面方法主要有三种 第一种方法:向服务器的动态页面发送请求,获取页面的html代码.这种方法缺点显而易见:速度慢.另外如果请求的动态页面有验证控件的话,返回的html页面却无 ...
- PHP代码为什么不能直接保存HTML文件——>PHP生成静态页面教程
1.server会依据文件的后缀名去进行解析,假设是HTML文件则server不会进行语法解析.而是直接输出到浏览器. 2.假设一个页面中所有都是HTML代码而没有须要解析的PHP语法,则没有必要保存 ...
随机推荐
- JSP—cookie
cookie的作用: 1.对特定对象的追踪,如访问次数,最后访问时间,路径等 2.统计网页的浏览次数 3.在cookie有效期内,记录用户的登录信息 4.实现个性化,记录用户的喜好 5.保存的数据存在 ...
- Object-C-Foundation-数组排序
系统类型排序; NSArray *goodsNames =@[@"computer",@"iphone",@"ipad"]; NSArray ...
- 查看Tensorflow版本
python -c 'import tensorflow as tf; print(tf.__version__)' # for Python 2 python3 -c 'import tensorf ...
- Java SE 基础知识(String,Array)
String 类: 1. 对于String对象的相等性判断来说,请使用equals()方法,而不是==.String的equals()是判断当前字符串与传进来的字符串的内容是否一致. 2. Strin ...
- Window下PHP三种运行方式图文详解,window下的php是不是单进程的?
Window下PHP三种运行方式图文详解,window下的php是不是单进程的? PHP运行目前为止主要有三种方式: a.以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成到Apache ...
- 20145212罗天晨 逆向及Bof基础实践
20145212罗天晨<网络对抗>第1周学习总结--逆向及Bof基础实践 逆向及Bof基础实践 一.实践目标 1.运行原本不可访问的代码片段 2.强行修改程序执行流 3.以及注入运行任意代 ...
- 探索Java8:(二)Function接口的使用
Java8 添加了一个新的特性Function,顾名思义这一定是一个函数式的操作.我们知道Java8的最大特性就是函数式接口.所有标注了@FunctionalInterface注解的接口都是函数式接口 ...
- VC++使用IMAPI调用Outlook邮箱客户端和Foxmail邮箱客户端遇到的问题
http://www.cnblogs.com/abiao/articles/303090.html 发送邮件 MAPISendMail() 发送邮件功能就是对MAPISendMail()的封装.下面解 ...
- <OFFER05> 05_ReplaceSpaces
void ReplaceBlank(char str[], int length) // length >= the real length of string { ) { return; } ...
- win10中mount和unmount iso文件
https://www.windowscentral.com/how-mount-or-unmount-iso-images-windows-10 You can also right-click t ...