深入淺出FreeMarker(二)-高級教程
深入淺出FreeMarker(二)-高級教程Peter Wei2012-3-6搜狐技術(shù)部-我的搜狐i.sohu.comFreemarker 的功能MVC 框架中的View 層組件 Html 頁面靜態(tài)
深入淺出FreeMarker(二)-高級教程
Peter Wei
2012-3-6
搜狐技術(shù)部-我的搜狐
i.sohu.com
Freemarker 的功能
MVC 框架中的View 層組件 Html 頁面靜態(tài)化 代碼生成工具 CMS 模板引擎 頁面欄目動態(tài)定制
基礎(chǔ)教程
深入淺出FreeMarker(一)-基礎(chǔ)教程
FreeMarker 介紹
開發(fā)指南-5分鐘入門
開發(fā)進階
實例應(yīng)用講解(我的搜狐marco 宏以及l(fā)ayout 布局的使用) 常用FreeMarker 資源
,分享目標
掌握macro 宏的使用
掌握freemarker 在MVC 框架View 層中的使用細節(jié)
掌握freemarker 結(jié)合springmvc 的異常處理
深入了解freemarker 在頁面靜態(tài)化以及cms 中的使用,達到綜合應(yīng)用的能力。
Macro 宏的使用
個人展示頁url 應(yīng)用地址生成,有個性域名及沒有的規(guī)則不一樣。
宏showUrl.ftl
<#--
Description:根據(jù)domain 或者upt 、接入方式、appname 動態(tài)生成url Author: Peter Wei (mailto:guangbowei@sohu-inc.com) -->
<#--
拼裝個人展示地址:因為會存在沒有domain 的用戶
domain:個性域名 upt:經(jīng)加密后的url passport
host:主機
accessType:接入方式
appName:如blog,mblog ,video 等
-->
host="i.sohu.com" <#macro showUrl domain=show_domain!"" upt =show_upt!""
accessType="index" appName="index" > <#escape x as (x!)?html >
<#if domain!="" >
<#if accessType=="proxy " > http://${domain}. ${host}/${appName}/<#t> http://${domain}. ${host}/app/${appName}/<#t> http://${domain}. ${host}/<#t> <#elseif accessType=="appview " > <#else> #if>
,<#elseif upt!="" >
<#if accessType==" proxy " > http://${host}/p/${upt}/${appName}/<#t> http://${host}/p/${upt}/app/${appName}/<#t> http://${host}/p/${upt}/<#t> <#elseif accessType=="appview " > <#else> #if>
#if>
#escape>
#macro>
Head.ftl
<#import "component/showUrl.ftl" as c>
生成界面
- 首頁
href="/upload/12/0zv40ppl4ltmblog/index.htm">微博
href="/upload/12/0zv40ppl4ltblog/index.htm">博客
href="/upload/12/0zv40ppl4ltalbum/index.htm">相冊
href="/upload/12/0zv40ppl4ltvideo/index.htm">播客
href="/upload/12/0zv40ppl4ltscomment/index.htm">我來說兩句
href="/upload/12/0zv40ppl4ltapp/baby/">育兒
href="/upload/12/0zv40ppl4ltapp/wenda/">問答
href="javascript:void(0);">更多
Freemarker 使用細節(jié)
Html 轉(zhuǎn)義
<#escape x as (x!)?html>
#escape>
變量及全局變量
,<#assign soml_follow="
<#assign soml_grade="
<#assign soml_visitor="
<#assign soml_interested="
<#assign soml_follow_user="
<#assign follow>
#assign>
<#--JS 版本號-->
<#global version="201202022018" >
<#t>標簽
源代碼清除空白不換行標簽
<#if accessType=="proxy " > http://${host}/p/${upt}/${appName}/<#t> http://${host}/p/${upt}/app/${appName}/<#t> http://${host}/p/${upt}/<#t> <#elseif accessType=="appview " > <#else> #if>
,List 索引_index
class="img-list1" >
- <#if
root.subList[0]?exists&&root.subList[0].productList?exists>
<#list root.subList[0].productList as product> <#if (product_index>=6)>
<#if (product_index>=9)> <#break> #if> #if>
#list>
#if>
Java 常量及枚舉類型的使用
常量
/**
* 布局類型
*/
public static final String LAYOUT_TYPE = "layout_type";
/**
* 默認布局, 內(nèi)容區(qū)通欄
*/
public static final String DEFAULT_LAYOUT = "2" ;
/**
* 兩列布局,內(nèi)容區(qū)分左右
*/
public static final String TWO_COLUMN_LAYOUT = "2" ;
,ConfigInterceptor
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws
{ ModelAndView modelAndView) throws Exception {
BeansWrapper bw = SimpleObjectWrapper.getDefaultInstance (); TemplateHashModel statics = bw.getStaticModels();
modelAndView.addObject("statics" , statics);
}
Exception
ftl 頁面調(diào)用
<#assign
PersonConstant=statics?if_exists["com.sohu.suc.person.constants.PersonConstant"]>
{PersonConstant.TWO_COLUMN_LAYOUT}
SpringMVC 中的配置
class ="org.springframework.web.servlet.view.freemarker.FreeMarker
key="auto_import">/common/index.ftl key="tag_syntax">auto_detect
異常處理
freemarker 提供了一個支持其錯誤處理機制的接口 TemplateExceptionHandler ,需要自己去擴展實現(xiàn),構(gòu)造自己的處理freemarker 模板錯誤的規(guī)范。
主要分為兩個部分
,實現(xiàn)接口
public class FreemarkerExceptionHandler implements
TemplateExceptionHandler {
private static final Logger log =
Logger. getLogger (FreemarkerExceptionHandler.class );
/* (non-Javadoc)TemplateException
* @see
freemarker.template.TemplateExceptionHandler#handleTemplateException(freemarker.template.TemplateException, freemarker.core.Environment, java.io.Writer)
*/
@Override
public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
try {
out.write("[Freemarker Error: " te.getMessage() "]"); log .warn("[Freemarker Error: " te.getMessage() "]"); } catch (IOException e) {
log .warn(e.getMessage());
throw new TemplateException("Failed to print error message. Cause: " e, env);
}
}
}
接口加入spring freemarker配置中
key="template_exception_handler">com.sohu.suc.plaza.web.util.FreemarkerExceptionHandler
頁面靜態(tài)化
為什么要靜態(tài)化
互聯(lián)網(wǎng),特別是門戶網(wǎng)站,用戶量和請求量都特別大。 如何解決高并發(fā)高負載的問題
常規(guī)的手段
● CDN
● 負載均衡(nginx)
● 應(yīng)用緩存(memcached,redis) ● 分庫分表
● 圖片服務(wù)器分離
● HTML 靜態(tài)化
應(yīng)用Cache 再快,也快不過html 靜態(tài)化頁面
靜態(tài)內(nèi)容CDN 系統(tǒng)
圖片、CSS 、JS 腳本、html 靜態(tài)化頁面