成人AV在线无码|婷婷五月激情色,|伊人加勒比二三四区|国产一区激情都市|亚洲AV无码电影|日av韩av无码|天堂在线亚洲Av|无码一区二区影院|成人无码毛片AV|超碰在线看中文字幕

深入淺出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>

,

<#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>

Head.ftl

<#import "component/showUrl.ftl" as c>

,

生成界面

Freemarker 使用細節(jié)

Html 轉(zhuǎn)義

<#escape x as (x!)?html>

變量及全局變量

,

<#assign soml_follow="">

<#assign soml_grade="">

<#assign soml_visitor="">

<#assign soml_interested="">

<#assign soml_follow_user="">

<#assign follow>

<#--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>

,

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)>

  • ${product.productName?if_exists}

  • <#if (product_index>=9)> <#break>

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

/WEB-INF/pages/ true GBK

,

ignore 300 yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm:ss # true,false true -->

key="auto_import">/common/index.ftl

key="tag_syntax">auto_detect

GBK -->

異常處理

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)化頁面

標簽: