Velocity生成靜態頁面

        隨着網站一步步的發展,網站的靜態化越來越重要。針對於目前市場上存在Velocity、FreeMarker、JSP三大模板引擎技術之一的Velocity技術進行分析,製作出一個關於Velocity實現頁面靜態化的Demo,供大家參考。

       Velocity的詳細介紹在本文之前已經詳細的介紹過,具體的簡介大家可以自行百度或者谷歌,我在這裏講一下主要的步驟:

       首先在生成頁面之前我們需要有對應的模板,即velocity能識別的文件.vm格式的模板,此文件需要我們按照所需要的靜態內容進行相應velocity標籤的書寫,之後才能使得是正確的生成靜態文件,詳細的代碼以及效果如下(PS:作者對於css的理解沒有那麼深刻,急需前端大神一枚,結對編程)。

java端代碼如下:

package com.forlink.etrade.common.trade.b2b.test;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

/**
 * Velocity生成靜態頁面
 * @author wanghjbuf 
 */
public class GenerHtmlForVelocity {
	private static final String DEF_ENCODING = "GBK";
	
	public static void main(String []args){
		GenerHtmlForVelocity generHtmlForVelocity = new GenerHtmlForVelocity();
		generHtmlForVelocity.doRelease();
	}

	/**
	 * 生成頁面主體
	 * @author wanghjbuf 
	 */
	public void doRelease(){
		try {  
			Properties p = initVelocityProperties();
		    Velocity.init(p);  
		    
		    Template template = getVelocityTemplate();  
		    VelocityContext context = initVelocityContext();  
		    BufferedWriter writer = getWriterStream();
		   
		    template.merge(context, writer);  
		    writer.close();  
		} catch (Exception e) {  
		    e.printStackTrace();  
		}  
	}
	
	/**
	 * 初始化VelocityProperties
	 * @author wanghjbuf 
	 */
	public Properties initVelocityProperties() throws Exception{
		Properties p = new Properties();  
		try{
			p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");  
			p.setProperty(Velocity.ENCODING_DEFAULT, DEF_ENCODING);  
			p.setProperty(Velocity.INPUT_ENCODING, DEF_ENCODING);  
			p.setProperty(Velocity.OUTPUT_ENCODING, DEF_ENCODING); 
		}catch(Exception e){
			e.printStackTrace();
		}
		return p;
	}
	
	/**
	 * 初始化VelocityContext
	 * @author wanghjbuf 
	 */
	public VelocityContext initVelocityContext() throws Exception{
	    VelocityContext context = new VelocityContext();  
		try{
		    List<String> blog_list = new ArrayList<String>();
		    blog_list.add("wanghjtheory");
		    blog_list.add("計算機組成原理");
		    blog_list.add("JDK");
		    blog_list.add("Oracle");
		    blog_list.add("Tomcat");
		    blog_list.add("Log4j");
		    blog_list.add("FastJson");
		    blog_list.add("Json");
		    blog_list.add("反射");
		    blog_list.add("SSI");
		    blog_list.add("Velocity");
		    blog_list.add("論文");
		    blog_list.add("os X");
		    blog_list.add("Js");
		    blog_list.add("FreeMarker");
		    blog_list.add("JSP");
		    
		    context.put("name", "wanghjbuf");  
		    context.put("blog_addr", "http://blog.csdn.net/wanghjbuf");
		    context.put("blog_list", blog_list);
		    context.put("last_modif", new Timestamp(System.currentTimeMillis()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return context;
	}
	
	/**
	 * 初始化VelocityContext
	 * @author wanghjbuf
	 * @param  String author_name
	 *         List<String> blog_list
	 */
	@SuppressWarnings("rawtypes")
	public VelocityContext initVelocityContext(String author_name,List blog_list) throws Exception{
	    VelocityContext context = new VelocityContext();  
		try{
		    context.put("name", author_name);  
		    context.put("blog_addr", "http://blog.csdn.net/"+author_name);
		    context.put("blog_list", blog_list);
		    context.put("last_modif", new Timestamp(System.currentTimeMillis()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return context;
	}
	
	/**
	 * 獲取Velocity模板
	 * @author wanghjbuf 
	 */
	public Template getVelocityTemplate() throws Exception{
		Template template = new Template();
		try{
			template = Velocity.getTemplate("G:/cvs_workspace/PD_XHCP_CODE/PD_XHCP_CODE/v4.000/etrade_b2b/webapp-etrade/src/main/webapp/webpage/etrade/pages/velocitytemplate/myblog.vm");
		}catch(Exception e){
			e.printStackTrace();
		}
		return template;
	}
	
	/**
	 * 獲取Velocity模板
	 * @author wanghjbuf 
	 * @param String velocityTemplateSource
	 */
	public Template getVelocityTemplate(String velocityTemplateSource) throws Exception{
		Template template = new Template();
		try{
			template = Velocity.getTemplate(velocityTemplateSource);
		}catch(Exception e){
			e.printStackTrace();
		}
		return template;
	}
	
	/**
	 * 獲取Velocity寫入流
	 * @author wanghjbuf 
	 */
	public BufferedWriter getWriterStream() throws Exception{
		try{
		    FileOutputStream fos = new FileOutputStream("G:/cvs_workspace/PD_XHCP_CODE/PD_XHCP_CODE/v4.000/etrade_b2b/webapp-etrade/src/main/webapp/webpage/etrade/pages/velocitydestination/myblog.html");  
		    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, DEF_ENCODING));
		    return writer;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 獲取Velocity寫入流
	 * @author wanghjbuf 
	 * @param String writerStreamSource
	 */
	public BufferedWriter getWriterStream(String writerStreamSource) throws Exception{
		try{
		    FileOutputStream fos = new FileOutputStream(writerStreamSource);  
		    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, DEF_ENCODING));
		    return writer;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}

模板文件如下:

<html>
  <head>
     <title>$!{name}的blog</title>
  </head>
  <body>
     <h1>Welcome $!{name} to velocity.com</h1>
     <h2><a href="$!{blog_addr}">Url $!{blog_addr}</a></h2>
     <h3>博文列表如下:</h3>
	     #foreach ($iten in $!{blog_list})
	       <h4>$!{iten}</h4>
	     #end
     <h2>Creat Time ${last_modif}</h2>
  </body>
</html>


生成的靜態頁面如下:

<html>
  <head>
     <title>wanghjbuf的blog</title>
  </head>
  <body>
     <h1>Welcome wanghjbuf to velocity.com</h1>
     <h2><a href="http://blog.csdn.net/wanghjbuf">Url http://blog.csdn.net/wanghjbuf</a></h2>
     <h3>博文列表如下:</h3>
	     	       <h4>wanghjtheory</h4>
	     	       <h4>計算機組成原理</h4>
	     	       <h4>JDK</h4>
	     	       <h4>Oracle</h4>
	     	       <h4>Tomcat</h4>
	     	       <h4>Log4j</h4>
	     	       <h4>FastJson</h4>
	     	       <h4>Json</h4>
	     	       <h4>反射</h4>
	     	       <h4>SSI</h4>
	     	       <h4>Velocity</h4>
	     	       <h4>論文</h4>
	     	       <h4>os X</h4>
	     	       <h4>Js</h4>
	     	       <h4>FreeMarker</h4>
	     	       <h4>JSP</h4>
	          <h2>Creat Time 2016-07-31 00:00:30.763</h2>
  </body>
</html>


效果圖如下:



*以上項目全部得到驗證,歡迎大家的指正


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章