模板引擎:第三章:使用Freemark做企業(yè)級SEO推廣
為什么做SEO?
當(dāng)公司項(xiàng)目已接近尾聲時,這個時候就需要推廣了,這個時候就需要SEO了,那SEO是什么呢?seo中文翻譯是搜索引擎優(yōu)化。它利用搜索引擎的規(guī)則提高網(wǎng)站在有關(guān)搜索引擎內(nèi)的自然排名。目的是讓其在行業(yè)內(nèi)占據(jù)領(lǐng)先地位,獲得品牌收益。很大程度上是網(wǎng)站經(jīng)營者的一種商業(yè)行為,將自己或自己公司的排名前移。
常見誤區(qū):
在速度方面,一般情況下,靜態(tài)網(wǎng)頁都要快于動態(tài)網(wǎng)頁。但是很多時候,我們?yōu)榱薙EO將網(wǎng)站全部搞成靜態(tài)化,有這個必要么?我們知道,現(xiàn)在網(wǎng)站做成純靜態(tài)的基本上不太可能,除了一些像維基百乎之類的網(wǎng)站,我們的網(wǎng)站都需要去修改,需要用戶去參與互動,還有一大堆的原因讓我們的網(wǎng)站都在動態(tài)的變化。結(jié)果我們就犧牲用戶的友好體驗(yàn)來“博得搜索引擎的歡心”。但是你可知道搜索引擎并不是喜歡靜態(tài)頁。
搜索引擎并不是喜歡靜態(tài)頁的起源:
網(wǎng)頁靜態(tài)化這個東西,純屬以訛傳訛的事。我們要說說這個事情的起源,在搜索引擎剛剛起步的時候,那個時間動態(tài)頁面剛剛興起,很多網(wǎng)頁的地址后面都帶有一大堆的參數(shù),并且這些參數(shù)可能是動態(tài)變化的,他會根據(jù)用戶的操作不同而有不同的參數(shù)。對于這樣的地址,搜索引擎是不喜歡的,這是為什么呢?我們先看搜索引擎在做什么事,搜索引擎實(shí)際上是一個程序,學(xué)名叫Spider,放出去之后把某個網(wǎng)站下載進(jìn)行分析,然后把摘要部分放到自己的數(shù)據(jù)庫,下次用戶搜索的時候,就直接搜索到它的數(shù)據(jù)庫了。
搜索引擎的工作原理:
比如說Google要來索引我的網(wǎng)站,它首先會訪問http://iove.net這個主域名,而實(shí)際上 http://iove.net是定位到http://iove.net/index.php這個頁面的。這樣spider首先下載index.php當(dāng)前頁的html內(nèi)容,注意,spider只需要原生的html內(nèi)容(當(dāng)然包括文本),不包括iframe中的任何內(nèi)容,也不包括js等腳本生成的任何內(nèi)容,也不會包括圖片、flash等多媒體的內(nèi)容,僅僅是對html標(biāo)簽和標(biāo)簽中的內(nèi)容進(jìn)行索引。它當(dāng)你的html是一段文本,然后進(jìn)行分析,取得其中的文字內(nèi)容和鏈接內(nèi)容,再根據(jù)這個鏈接進(jìn)行下一個索引。搜索引擎實(shí)際就是把文本進(jìn)行分析,然后放到數(shù)據(jù)庫。注意數(shù)據(jù)庫!有些技術(shù)基礎(chǔ)的人都知道,只要是數(shù)據(jù)庫,就會有主鍵,而根據(jù)范式理論,數(shù)據(jù)庫應(yīng)該有一個唯一的主鍵,那么搜索引擎索引回去的數(shù)據(jù),也應(yīng)該有一個唯一的主鍵。這個主鍵是什么?就是我們的網(wǎng)址。
好了,理論說完,開搞
pom.xml引入freemark相關(guān)依賴:
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.23</version>
- </dependency>
SpringMvc配置文件:
- <!-- 注冊freemarker配置類 -->
- <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <!-- ftl模版文件路徑 -->
- <property name="templateLoaderPath" value="classpath:freemarker/"></property>
- <!-- 頁面編碼 -->
- <property name="defaultEncoding" value="utf-8" />
- <property name="freemarkerSettings">
- <props>
- <!-- 模版緩存刷新時間,不寫單位默認(rèn)為秒 -->
- <prop key="template_update_delay">0</prop>
- <!-- 時區(qū) 和 時間格式化 -->
- <prop key="locale">zh_CN</prop>
- <prop key="datetime_format">yyyy-MM-dd</prop>
- <prop key="date_format">yyyy-MM-dd</prop>
- <!-- 數(shù)字使用.來分隔 -->
- <prop key="number_format">#.##</prop>
- </props>
- </property>
- <!--靜態(tài)資源訪問路徑-->
- <property name="freemarkerVariables">
- <map>
- <entry key="resPath" value="${resources.server}"/>
- </map>
- </property>
- </bean>
- <!-- 注冊freemarker視圖解析器 -->
- <bean id="freeMarkerViewResolver"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <!-- 視圖解析順序,排在其他視圖解析器之后 數(shù)字越大優(yōu)先級越低 -->
- <property name="order" value="0" />
- <!-- 開啟模版緩存 -->
- <property name="cache" value="true" />
- <!-- 上面已經(jīng)配了,這里就不用配啦 -->
- <property name="prefix" value="" />
- <!-- 配置文件后綴 -->
- <property name="suffix" value=".ftl" />
- <property name="contentType" value="text/html;charset=UTF-8" />
- <!-- 是否允許session屬性覆蓋模型數(shù)據(jù),默認(rèn)false -->
- <property name="allowSessionOverride" value="false" />
- <!-- 是否允許request屬性覆蓋模型數(shù)據(jù),默認(rèn)false -->
- <property name="allowRequestOverride" value="false" />
- <!-- 開啟spring提供的宏幫助(macro) -->
- <property name="exposeSpringMacroHelpers" value="true" />
- <!-- 添加request attributes屬性到ModelAndView中 -->
- <property name="exposeRequestAttributes" value="true" />
- <!-- 添加session attributes屬性到ModelAndView中 -->
- <property name="exposeSessionAttributes" value="true" />
-
- <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
- <property name="requestContextAttribute" value="request" />
- </bean>
application.properties配置文件:
#靜態(tài)路徑配置
resources.server=http://127.0.0.1:8081/tst_consumer_war_exploded/
一般而言,我們只需要將后端的數(shù)據(jù)響應(yīng)給前端模板頁面即可。
關(guān)于靜態(tài)資源路徑問題:在SpringMvc的配置中加上:
<mvc:resources mapping="/img/**" location="classpath:freemarker/img/"></mvc:resources>
而模板頁面一般以.ftl結(jié)尾,頁面內(nèi)容類似:
至于生成靜態(tài)模板頁面,這里提供一個鏈接include 引用模板:https://www.cnblogs.com/fangwu/p/8696443.html
提供一個生成靜態(tài)html文件的方法:
/**
* 生成靜態(tài)頁面
* @param data 模板數(shù)據(jù) 創(chuàng)建一個穆數(shù)據(jù)集,可以是pojo也可以是map,推薦使用map
* @param ftlPath 模板路徑
例如:E:\\Project\\CompanyProject\\standard\\tst-consumer\\src\\main\\resources\\freemarker\\aboutUs
* @param ftlName 模板名稱 例如:index.ftl
* @param htmlPath 生成的靜態(tài)文件路徑
例如:E:\\Project\\CompanyProject\\standard\\tst-consumer\\src\\main\\resources\\freemarker\\staticHtml
* @param htmlName 生成的靜態(tài)名稱 例如:aboutBaijian.html
* @throws Exception
*/
public static void toHtml(Map<String,Object> data,String
ftlPath,String ftlName,String htmlPath,String htmlName) throws
Exception{
// 創(chuàng)建一個Configuration對象
Configuration configuration = new Configuration(Configuration.getVersion());
// 設(shè)置模板文件保存的目錄
configuration.setDirectoryForTemplateLoading(new File(ftlPath));
// 設(shè)置文件的編碼格式,一般是utf-8
configuration.setDefaultEncoding("utf-8");
configuration.setClassicCompatible(true);
// 加載一個模板文件,創(chuàng)建一個模板對象
Template template = configuration.getTemplate(ftlName);
// 創(chuàng)建一個Writer對象,指定輸出文件的路徑以及文件名
Writer out = new FileWriter(new File(htmlPath + "\\" + htmlName));
// 生成靜態(tài)頁面
template.process(data, out);
// 關(guān)閉流
out.close();
}
注意:如果模板文件使用<#include "additional.ftl">包含另外的模板二個模板要在同一個目錄中,如:
我在aboutUs目錄下的index.ftl使用了include包含comment目錄下的文件,生成的時候會報(bào)錯,因?yàn)槟闼峁┑哪0迥夸浿挥幸粋€,而你卻引用了二個目錄,模板找不到會報(bào)錯。
freemark最常見的用法,這個提供鏈接:https://blog.csdn.net/qq_35376421/article/details/81095656