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