网页静态化的了解
网页静态化有很多含义,在WEB开发中,静态网页一般理解为网站中大部分超级链接所引用的页面是单独的HTML静态页面文件(如*.htm、*.html等页面文件)。在ASP.NET中(ASP、PHP或JSP也一样),页面都是通过运行库(其他语言也有相应的解释器或运行环境)动态生成HTML代码,然后发送到浏览器端的,而不是直接将页面文件发送。通过一定的技术手段,将浏览用户通过超级链接可能浏览到的页面内容预先转换为单独的HTML静态页面,当用户浏览时,服务器直接将该页面文件发送到浏览器端解析。
网页静态化的好处具体有以下几点
在某些情况下,用户请求浏览页面时,由于服务器不需要运行库动态生成HTML、访问数据库等操作,可以明显提高页面浏览的速度,减轻服务器的运行负担。所以网页静态化特别适用于大型网站,大量用户请求页面时,服务器不需要ASP.NET运行库的工作及数据库的访问,减少了大量的页面输出工作。不过某些情况也不适合网页静态化,因为网页静态化本身就是ASP.NET程序动态生成HTML代码并存储为单独的物理文件到磁盘,这个过程过于频繁将导致服务器压力过大。在这方面,编程者要根据WEB项目的实际需要,权衡是否需要使用网页静态化技术。
有利于搜索引擎收录网站页面信息。商业运营网站特别渴求Google、百度之类的搜索引擎能尽量多地收录站点中页面的信息,HTML页面能使网站页面收录的可能性增加很多。还有一种类似的技术也可以完成类似的功能,即网页伪静态化,即通过URL重写等技术使超级链接所指向的静态HTML地址转向动态页面(如*.aspx页面)。因为搜索引擎很多时候首先搜索页面中的超级链接,超级链接所指向的是HTML静态页面,所以可以很轻易地直接搜索到所对应的动态页面内容。
Test.ftl
<html>
<head>
<meta charset="utf-8">
<title>Freemarker入门小DEMO </title>
</head>
<body>
<#--我只是一个注释,我不会有任何输出 -->
${name},你好。${message}
<h3>assigne指令</h3>
<#assign linkman="周先生">
联系人:${linkman}
<#assign info={"mobile":"13301231212",'address':'北京市昌平区王府街'} >
电话:${info.mobile} 地址:${info.address}
<h3>if指令</h3>
<#if success=true>
你已通过实名认证
<#else>
你未通过实名认证
</#if>
<h3>list指令</h3>
----商品价格表----<br>
<#list goodsList as goods>
${goods_index+1} 商品名称: ${goods.name} 价格:${goods.price}<br>
</#list>
<h3>内建函数</h3>
<h4>获取集合大小</h4>
共 ${goodsList?size} 条记录
<h4>转换JSON字符串为对象</h4>
<#assign text="{'bank':'工商银行','account':'10101920201920212'}" />
<#assign data=text?eval />
开户行:${data.bank} 账号:${data.account}
<h4>日期格式化</h4>
当前日期:${today?date} <br>
当前时间:${today?time} <br>
当前日期+时间:${today?datetime} <br>
日期格式化: ${today?string("yyyy年MM月")}
<h4>数字转换为字符串</h4>
累计积分:${point}
累计积分:${point?c}
<h3>空值处理运算符</h3>
<h4>判断某变量是否存在:“??”</h4>
<#if aaa??>
aaa变量存在
<#else>
aaa变量不存在
</#if>
<h4>缺失变量默认值:“!”</h4>
${aaa!'-'}
<h3>运算符</h3>
<h4>算数运算符</h4>
FreeMarker表达式中完全支持算术运算,FreeMarker支持的算术运算符包括:+, - , * , / , %
<h4>逻辑运算符</h4>
逻辑运算符有如下几个:
逻辑与:&&
逻辑或:||
逻辑非:!
逻辑运算符只能作用于布尔值,否则将产生错误
<h4>比较运算符</h4>
表达式中支持的比较运算符有如下几个:
1 =或者==:判断两个值是否相等.
2 !=:判断两个值是否不等.
3 >或者gt:判断左边值是否大于右边值
4 >=或者gte:判断左边值是否大于等于右边值
5 <或者lt:判断左边值是否小于右边值
6 <=或者lte:判断左边值是否小于等于右边值
注意: =和!=可以用于字符串,数值和日期来比较是否相等,但=和!=两边必须是相同类型的值,否则会产生错误,而且FreeMarker是精确比较,"x","x ","X"是不等的.其它的运行符可以作用于数字和日期,但不能作用于字符串,大部分的时候,使用gt等字母运算符代替>会有更好的效果,因为 FreeMarker会把>解释成FTL标签的结束字符,当然,也可以使用括号来避免这种情况,
</body>
</html>
后台类
public class Demo001 {
public static void main(String[] args) throws IOException, TemplateException {
// 1.创建配置类
Configuration configuration = new Configuration(Configuration.getVersion());
// 2.设置模板所在的目录
configuration.setDirectoryForTemplateLoading(
new File("D:\\17、freemarker页面静态化(1)~番外篇\\20190902095231\\javaxl_lunece_freemarker\\src\\main\\resources"));
// 3.设置字符集
configuration.setDefaultEncoding("utf-8");
// 4.加载模板
Template template = configuration.getTemplate("test.ftl");
// 5.创建数据模型
Map map = new HashMap();
map.put("name", "小李飞刀 ");
map.put("message", "欢迎来到神奇的博客网站:http://www.javaxl.com!");
map.put("success", true);
List goodsList=new ArrayList();
Map goods1=new HashMap();
goods1.put("name", "苹果");
goods1.put("price", 5.8);
Map goods2=new HashMap();
goods2.put("name", "香蕉");
goods2.put("price", 2.5);
Map goods3=new HashMap();
goods3.put("name", "橘子");
goods3.put("price", 3.2);
goodsList.add(goods1);
goodsList.add(goods2);
goodsList.add(goods3);
map.put("goodsList", goodsList);
map.put("today", new Date());
map.put("point", 102920122);
// 6.创建Writer对象
Writer out = new FileWriter(new File("D:\\17、freemarker页面静态化(1)~番外篇\\20190902095231\\javaxl_lunece_freemarker\\src\\main\\webapp\\freemarker\\test.html"));
// 7.输出
template.process(map, out);
// 8.关闭Writer对象
out.close();
}
}
运行生成
blogDetail.ftl
<div class="data_list">
<div class="data_list_title">
<img src="../blog_show_icon.png"/>
博客信息
</div>
<div>
<div class="blog_title"><h3><strong>${blog.title }</strong></h3></div>
<div class="blog_share">
<!-- <div class="bshare-custom"><a title="分享到QQ空间" class="bshare-qzone"></a><a title="分享到新浪微博" class="bshare-sinaminiblog"></a><a title="分享到人人网" class="bshare-renren"></a><a title="分享到腾讯微博" class="bshare-qqmb"></a><a title="分享到网易微博" class="bshare-neteasemb"></a><a title="更多平台" class="bshare-more bshare-more-icon more-style-addthis"></a><span class="BSHARE_COUNT bshare-share-count">0</span></div><script type="text/javascript" charset="utf-8" src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=2&lang=zh"></script><script type="text/javascript" charset="utf-8" src="http://static.bshare.cn/b/bshareC0.js"></script> -->
</div>
<div class="blog_info">
发布时间:『${blog.releaseDate?datetime }』 博客类别:${blog.btid } 阅读(${blog.clickHit })
</div>
<div class="blog_content">
${blog.content }
</div>
</div>
</div>
后台 Demo002
public class Demo002 {
@SuppressWarnings("rawtypes")
public static void main(String[] args) throws IOException, TemplateException, SQLException {
// 1.创建配置类
Configuration configuration = new Configuration(Configuration.getVersion());
// 2.设置模板所在的目录
configuration.setDirectoryForTemplateLoading(
new File("D:\\17、freemarker页面静态化(1)~番外篇\\20190902095231\\javaxl_lunece_freemarker\\src\\main\\webapp\\freemarker"));
// 3.设置字符集
configuration.setDefaultEncoding("utf-8");
// 4.加载模板
Template template = configuration.getTemplate("blogDetail.ftl");
// 5.创建数据模型
// Map map = new HashMap();
// map.put("name", "小李飞刀 ");
// // 6.创建Writer对象
// Writer out = new FileWriter(new File("E:\\temp\\staticPage\\test.html"));
// // 7.输出
// template.process(map, out);
// 8.关闭Writer对象
// out.close();
createPage(template);
}
private static void createPage(Template template) throws SQLException, IOException, TemplateException {
Connection con = DBAccess.getConnection();
String sql = "select * from t_lucene_freemarker_blog";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
Map map = new HashMap<>();
Map<String, Object> blog = new HashMap<>();
while(rs.next()) {
blog.put("bid", rs.getObject("bid"));
blog.put("title", rs.getObject("title"));
blog.put("releaseDate", rs.getObject("releaseDate"));
blog.put("btid", rs.getObject("btid"));
blog.put("clickHit", rs.getObject("clickHit"));
blog.put("content", rs.getObject("content"));
map.put("blog", blog);
// // 6.创建Writer对象
Writer out = new FileWriter(new File("D:\\17、freemarker页面静态化(1)~番外篇\\20190902095231\\javaxl_lunece_freemarker\\src\\main\\webapp\\freemarker\\"+blog.get("bid")+".html"));
// 7.输出
template.process(map, out);
// 8.关闭Writer对象
out.close();
}
DBAccess.close(con, pst, rs);
}
}
server.xml
blogList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form
action="${pageContext.request.contextPath}/sy/freemarkerBlog_list.action"
method="post">
博客标题:<input type="text" name="title"> <input type="submit"
value="确定">
</form>
<button id="add">添加</button>
<button id="refresh">刷新全局索引</button>
<table border="1" width="100%" cellspacing="0">
<tr>
<td>编号</td>
<td>标题</td>
<td>内容</td>
<td>操作</td>
</tr>
<c:forEach items="${blogList }" var="blog">
<tr>
<td>${blog.bid }</td>
<td>${blog.title }</td>
<!-- 不使用网页静态化 -->
<%-- <td><a target="_blank" href="${pageContext.request.contextPath}/sy/freemarkerBlog_show.action?bid=${blog.bid }">${blog.summary }</a></td> --%>
<!-- 使用网页静态化 -->
<td><a target="_blank" href="${pageContext.request.contextPath}/freemarker/${blog.bid }.html">${blog.summary }</a></td>
<td><a href="">修改</a> <a href="">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
结果