深入淺出FreeMarker(二)-高級(jí)教程
深入淺出FreeMarker(二)-高級(jí)教程Peter Wei2012-3-6搜狐技術(shù)部-我的搜狐i.sohu.comFreemarker 的功能MVC 框架中的View 層組件 Html 頁(yè)面靜態(tài)
深入淺出FreeMarker(二)-高級(jí)教程
Peter Wei
2012-3-6
搜狐技術(shù)部-我的搜狐
i.sohu.com
Freemarker 的功能
MVC 框架中的View 層組件 Html 頁(yè)面靜態(tài)化 代碼生成工具 CMS 模板引擎 頁(yè)面欄目動(dòng)態(tài)定制
基礎(chǔ)教程
深入淺出FreeMarker(一)-基礎(chǔ)教程
FreeMarker 介紹
開(kāi)發(fā)指南-5分鐘入門(mén)
開(kāi)發(fā)進(jìn)階
實(shí)例應(yīng)用講解(我的搜狐marco 宏以及l(fā)ayout 布局的使用) 常用FreeMarker 資源
,分享目標(biāo)
掌握macro 宏的使用
掌握f(shuō)reemarker 在MVC 框架View 層中的使用細(xì)節(jié)
掌握f(shuō)reemarker 結(jié)合springmvc 的異常處理
深入了解freemarker 在頁(yè)面靜態(tài)化以及cms 中的使用,達(dá)到綜合應(yīng)用的能力。
Macro 宏的使用
個(gè)人展示頁(yè)url 應(yīng)用地址生成,有個(gè)性域名及沒(méi)有的規(guī)則不一樣。
宏showUrl.ftl
<#--
Description:根據(jù)domain 或者upt 、接入方式、appname 動(dòng)態(tài)生成url Author: Peter Wei (mailto:guangbowei@sohu-inc.com) -->
<#--
拼裝個(gè)人展示地址:因?yàn)闀?huì)存在沒(méi)有domain 的用戶(hù)
domain:個(gè)性域名 upt:經(jīng)加密后的url passport
host:主機(jī)
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>
生成界面
- 首頁(yè)
href="/upload/12/0zv40ppl4ltmblog/index.htm">微博
href="/upload/12/0zv40ppl4ltblog/index.htm">博客
href="/upload/12/0zv40ppl4ltalbum/index.htm">相冊(cè)
href="/upload/12/0zv40ppl4ltvideo/index.htm">播客
href="/upload/12/0zv40ppl4ltscomment/index.htm">我來(lái)說(shuō)兩句
href="/upload/12/0zv40ppl4ltapp/baby/">育兒
href="/upload/12/0zv40ppl4ltapp/wenda/">問(wèn)答
href="javascript:void(0);">更多
Freemarker 使用細(xì)節(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 版本號(hào)-->
<#global version="201202022018" >
<#t>標(biāo)簽
源代碼清除空白不換行標(biāo)簽
<#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 常量及枚舉類(lèi)型的使用
常量
/**
* 布局類(lèi)型
*/
public static final String LAYOUT_TYPE = "layout_type";
/**
* 默認(rèn)布局, 內(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 頁(yè)面調(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 提供了一個(gè)支持其錯(cuò)誤處理機(jī)制的接口 TemplateExceptionHandler ,需要自己去擴(kuò)展實(shí)現(xiàn),構(gòu)造自己的處理freemarker 模板錯(cuò)誤的規(guī)范。
主要分為兩個(gè)部分
,實(shí)現(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
頁(yè)面靜態(tài)化
為什么要靜態(tài)化
互聯(lián)網(wǎng),特別是門(mén)戶(hù)網(wǎng)站,用戶(hù)量和請(qǐng)求量都特別大。 如何解決高并發(fā)高負(fù)載的問(wèn)題
常規(guī)的手段
● CDN
● 負(fù)載均衡(nginx)
● 應(yīng)用緩存(memcached,redis) ● 分庫(kù)分表
● 圖片服務(wù)器分離
● HTML 靜態(tài)化
應(yīng)用Cache 再快,也快不過(guò)html 靜態(tài)化頁(yè)面
靜態(tài)內(nèi)容CDN 系統(tǒng)
圖片、CSS 、JS 腳本、html 靜態(tài)化頁(yè)面