zoukankan      html  css  js  c++  java
  • FreeMarker静态化文件解决SEO推广问题

    1.问题背景

    SEO一直是站点对外推广的一个重要手段,如何可以让搜索引擎高速搜索到站点对于增强站点的浏量,提升站点对外形象有着重要意义。那么如何可以对SEO进行优化呢?一个很经常使用的手段就是在网页的keyword部分多添加可以表示本网页的keyword,而且这些keyword在接下来的网页正文中可以多次出现,另外一个重要的方法就是假设在本网页中存在文章链接。最好是每一个链接的地址是不一样的,这样的不一样不是指同一个链接传的參数不一样,而是每一个链接相应的都是一个新的html页面。假设你的页面是jsp那么我们所须要做的就是将将其进行伪静态化。在公司新平台推出之际。查询实现代码发现文章内容的现实都是採用ajax方式展示的,这样导致的问题就是查看网页源代码的时候根本查询不到文章内容。基于这个原因我们须要对内容展示部分进行又一次实现。也就是将文章内容进行静态化。

    2.详细方式

    对文章内容进行静态化,所採用的工具是freemarker,通过模板引擎生成jsp页面。首先我们须要一个ftl模板文件,这个文件就是模板引擎生成jsp页面的依据,其内容与html十分相似,仅仅只是是在某些部分通过变量用从后台传过来的数据进行填充,如以下的样例:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
    <%String path = request.getContextPath();%>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link rel = "Shortcut Icon" href="<%=path%>/resources/images/favicon.ico" >
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="keywords" content="${contentItem.keywords}"/> 
    <meta name="description" content="${contentItem.explanation}"/>
    <link href="<%=path%>/resources/css/style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="<%=path%>/resources/js/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="<%=path%>/resources/js/jquery/jQuery.md5.js"></script>
    <script type="text/javascript" src="<%=path%>/resources/js/common.js"></script>
    <script type="text/javascript" src="<%=path%>/resources/js/about.js"></script>
    <script charset="utf-8" src="http://wpa.b.qq.com/cgi/wpa.php"></script>
    <script type="text/javascript">
    </script>
    <title>关于我们-普惠理財</title>
    </head>
    <body>
    <!--页头start-->
    <jsp:include page="../frame_top.jsp"/>
    <!--页头end-->
    
    <div class="ny_content clearfix">
        
        <jsp:include page="leftMenu.html"/>	
        <!-- <div id="leftMenu"></div> -->
       	
       	<div class="ny_maincont fl" style="table-layout:fixed; word-break: break-all;">
            	<div class="position"><h4>${contentItem.title}</h4></div>
                
                	${contentItem.content}
                        
                </div>
                
            </div>
           
    </div>
    
    
    <!--页尾start-->
    <jsp:include page="../frame_bottom.jsp"/>
    <!--页尾end-->
    <jsp:include page="../rightContext.jsp"/>
    </body>
    </html>

    当中的${contentItem.content}就是从后台传入的须要展示的内容。接下来就是java部分:

    package com.voiinnov.puhuilicai.freemarker;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    
    public class FreeMarkerEngine {
    	private static Log logger = LogFactory.getLog(FreeMarkerEngine.class);
    	private Configuration freemarker_cfg = null;
    	
    	/**
    	 * 设置关键字,描写叙述,内容并调用
    	 * @param fileName 		       将要生成的jsp页面的名字
    	 * @param content  		   jsp页面的主体内容
    	 * @param keywords 		        关键字
    	 * @param explanation      说明
    	 * @param template         生成该jsp所使用的ftl模板的名字
    	 * @throws IOException
    	 */
    	public void getAbuoutUsJSP(String template,String fileName,String content,String keywords,String explanation,String sRootDir,String title) throws IOException{
    		
    		ContentItem contentItem = new ContentItem();
    		FreeMarkerEngine fme = new FreeMarkerEngine();
    		/*****设置关键词*******/
    		contentItem.setKeywords(keywords);
    		/*****设置描写叙述*****/
    		contentItem.setExplanation(explanation);
    		/****设置内容****/
    		contentItem.setContent(content);
    		/****设置大标题****/
    		contentItem.setTitle(title);
    		Map root = new HashMap();
    		root.put("contentItem",contentItem);
    		String sGeneFilePathRoot = "/";
    		boolean bOK = fme.geneJSPFile("/" + template, root, sGeneFilePathRoot, fileName,sRootDir);
    	}
    	
    
    
    	/**
    	 * 生成jsp或html页面
    	 * @param templateFileName
    	 * @param propMap
    	 * @param htmlFilePath    
    	 * @param htmlFileName
    	 * @param sRootDir   生成的jsp存放的路径
    	 * @return
    	 * @throws IOException
    	 */
    	//private boolean geneJSPFile(String templateFileName,Map propMap ,String jspFilePath,String jspFileName,String sRootDir) throws IOException{
    	public boolean geneJSPFile(String templateFileName,Map propMap ,String jspFilePath,String jspFileName,String sRootDir) throws IOException{
    
    		try{
    			Template t = getFreeMarkerCFG().getTemplate(templateFileName);
    			File afile = new File(sRootDir + "/" + jspFilePath + "/" + jspFileName);
    			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
    			t.process(propMap, out);
    		}catch(TemplateException e){
    			return false;
    		}
    		return true;
    	}
    	
    	/**
    	 * 读取ftl模板文件
    	 * @return
    	 */
    	private Configuration getFreeMarkerCFG(){
    		if(null == freemarker_cfg){
    			freemarker_cfg = new Configuration();
    			freemarker_cfg.setClassForTemplateLoading(this.getClass(), "/resources/ftl");
    		}
    		return freemarker_cfg;
    	}
    }
    

    整个运行过程也十分的简单,模板引擎通过读取模板文件确定要生成页面的格式。然后通过将变量的值填充到新文件的相应部分,实现动态的读取数据库中的内容。然后将页面静态化,将新生成的页面元原ajax方式填充的页面相比。你会发现尽管两种页面在展示效果上来看没有不论什么差别,可是查看源码ajax的内容部分是空白的,可是静态化后的文件内容部分是真是存在的,并且这个页面会成为一个独立链接的链接源。并非通常採用的同一个链接通过传递不同的參数显示不同的内容。这样的方式是十分有利于SEO的。


  • 相关阅读:
    Qt5官方demo解析集21——Extending QML
    多封装,少开放。强烈建议C++标准添加class之间的注入机制
    iOS 设计模式之工厂模式
    golang的select典型用法
    Go的异常处理 defer, panic, recover
    Visual Studio Code 的简单试用体验
    在Visual Studio Code中配置GO开发环境
    Go语言开发环境配置
    Go 语言 很牛
    Go将统治下一个10年?Go语言发展现状分析
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6740335.html
Copyright © 2011-2022 走看看