`
chensl
  • 浏览: 55832 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

FreeMarker静态化文件(转)

阅读更多

1.问题背景

SEO一直是网站对外推广的一个重要手段,如何能够让搜索引擎快速搜索到网站对于增强网站的浏量,提升网站对外形象有着重要意义。那么怎样能够对SEO进行优化呢?一个非常常用的手段就是在网页的关键字部分多增加能够表示本网页的关键字,并且这些关键字在接下来的网页正文中能够多次出现,另外一个重要的方法就是如果在本网页中存在文章链接,最好是每个链接的地址是不一样的,这种不一样不是指同一个链接传的参数不一样,而是每个链接对应的都是一个新的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的。

 

 

原文地址:http://blog.csdn.net/a1314517love/article/details/41143709

                 http://www.tuicool.com/articles/fMFb2y

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics