网站优化技术——页面静态化

目前,实现页面静态化比较好的技术有Velocity和FreeMarker。而FreeMarker是比Velocity晚出现的技术,从从效果上看也比Velocity好一点,但我个人则偏好Velocity,因为它的扩展性好,能实现绝大部分需求。所以选择了Velocity来简单讲解一下。

1、使用velocity需要的jar:有velocity-1.6-dep.jar和\lib\log4j-x.x.xx.jar;如果不使用velocity-1.6-dep.jar,也可以使用velocity-1.6.jar,但这时需要把commons-collections-x.x.x.jar、commons-lang-x.x.jar和oro-x.x.x.jar放入类路径下。velocity-1.6-dep.jar文件内部已经包含前面三个jar文件的类。

2、使用velocity的配置:在类路径下加入velocity.properties(名字可任意取),内容如下:
指定日志文件存放位置
runtime.log=E:\\spring\\velocity\\velocity_example.log
指定模版文件加载位置
file.resource.loader.path=E:\\spring\\velocity
指定输入编码格式
input.encoding=UTF-8
指定velocity的servlet向浏览器输出内容的编码(若使用velocity代替jsp层,则配置此项)
default.contentType=text/html;charset\=UTF-8
指定输出编码格式
output.encoding=UTF-8

3、这是Velocity不相关的,但面试时却常问到,就是:实现了页面静态化后,如何统计页面访问数量?
解决方案:下面是静态化后的页面
<html>
<body>
……显示的内容
<img src=”http://www.huhui.com/date/count.do?productid=11” width=0 heigth=0/> http://www.huhui.com为站点,原理是此图片会加载此路径对应的Action,然后统计该页面访问的数量,当然图片是显示不出来的
</body>
</html>

既然FreeMarker作为新技术,在附件中我给大家共享了一份FreeMarker学习资料(中文版),以供大家学习。

猜你喜欢

转载自javahuhui.iteye.com/blog/1479428