freemarker&velocity的使用


本篇文章为原创,转载请说明出处

 下面是我写的关于模板引擎的列子,希望对大家有所帮助

首先看下目录结构:


freemarker&velocity的使用


 所需要的jar包:

freemarker&velocity的使用

 

第一步:编写接口TemplateEngine.java

package templatetest.templateInterface;

import java.util.Map;

public interface TemplateEngine {

	void run(Map context) throws Exception;
}

 第二步:编写工具类Constants.java

package templatetest.util;

public class Constants {

	public static final String ENGINE_TYPE_VELOCITY = "volecity";
	public static final String ENGINE_TYPE_FREEMARKER  = "freemarker";
}

 第三步:编写抽象类AbstractTemplateEngine.java

package templatetest.templateImpl;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Properties;

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

import templatetest.templateInterface.TemplateEngine;
import templatetest.util.Constants;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public abstract class AbstractTemplateEngine implements TemplateEngine{

	public abstract String getTemplatePath();
	public abstract String getTemplate();
	public abstract String getEngineType();
	
	public void run(Map context) throws Exception {
		
		if(Constants.ENGINE_TYPE_FREEMARKER.equals(getEngineType())){
			executeFreemarker(context);
		}else{
			executeVelocity(context);
		}
	}
	
	private void executeFreemarker(Map context) throws Exception {
		Configuration cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File(getTemplatePath()));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));
		
		Template template = cfg.getTemplate(getTemplate());
		
		Writer write = new OutputStreamWriter(System.out);
		template.process(context, write);
		write.flush();
	}
	private void executeVelocity(Map context) throws Exception {
		
		Properties p = new Properties();
		p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, getTemplatePath());
		Velocity.init(p);
		
		VelocityContext velocityContext = new VelocityContext(context);
		org.apache.velocity.Template template = null;
		
		template = Velocity.getTemplate(getTemplate());
		StringWriter sw = new StringWriter();
		template.merge(velocityContext, sw);
		System.out.println(sw.toString());
	}
}

 

 

第四步:分别编写FreemarkerTemplateEngine.java和VelocityTemplateEngine.java

package templatetest.templateImpl;

import java.util.Map;

import templatetest.util.Constants;

public class FreemarkerTemplateEngine extends AbstractTemplateEngine {
	public static String DEFAULT_TEMPLATE = "FreemarkerExample1.ftl";

	@Override
	public String getEngineType() {
		return Constants.ENGINE_TYPE_FREEMARKER;
	}

	@Override
	public String getTemplate() {
		return DEFAULT_TEMPLATE;
	}

	@Override
	public String getTemplatePath() {
		return null;
	}
	public void run(Map context) throws Exception{
		super.run(context);
	}
}

 

package templatetest.templateImpl;

import java.util.Map;

import templatetest.util.Constants;

public class VelocityTemplateEngine extends AbstractTemplateEngine {
	public static String DEFAULT_TEMPLATE ="VelocityExample1.vm";

	@Override
	public String getEngineType() {
		return Constants.ENGINE_TYPE_VELOCITY;
	}

	@Override
	public String getTemplate() {
		return DEFAULT_TEMPLATE;
	}

	@Override
	public String getTemplatePath() {
		return null;
	}

	public void run(Map context) throws Exception{
		super.run(context);
	}

}

 

第五步:编写工厂类TemplateFactory.java

package templatetest.factory;

import java.util.HashMap;
import java.util.Map;

import javax.jms.ObjectMessage;

import templatetest.templateImpl.FreemarkerTemplateEngine;
import templatetest.templateImpl.VelocityTemplateEngine;

public class TemplateFactory {

	private static TemplateFactory instance;
	private static Map objectMap;
	
	static{
		instance = new TemplateFactory();
	}
	public TemplateFactory(){
		super();
		this.objectMap = new HashMap();
		synchronized(this){
			objectMap.put("freemarker", new FreemarkerTemplateEngine(){
				public String getTemplatePath(){
					return "D:/tomcat6/webapps/jc6/templatetest/";
				}
			});
			objectMap.put("velocity", new VelocityTemplateEngine(){
				public String getTemplatePath(){
					return "D:/tomcat6/webapps/jc6/templatetest/";
				}
			});
		}
		
		
	}
	
	public static TemplateFactory getInstance(){
		return instance;
	}
	public static Object getBean(String key){
		return objectMap.get(key);
	}
}

 

 第六步:编写模板

freemarker template test:
string test: ${user}---${map.url}----${map.name}
Long test:${age}
condition test:
<#if user == 'longzhun'>
list test:
	<#list numberList as aa>
		${aa}
	</#list>

</#if>

date test:${date?string("yyyy-MM-dd")}
date test:${date?string("MMM/dd/yyyy")}
date test:${date?string("")}2011-5-232011-5-23

 

velocity template test:
string test: ${user}---${map.url}----${map.name}
Long test:${age}
condition test:
#if($user == "longzhun")
list test:
	#foreach($aa in $numberList)
	     $aa
	#end

#end
date test:${date}

 第七步:准备数据,测试

package templatetest.test;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import templatetest.factory.TemplateFactory;
import templatetest.templateInterface.TemplateEngine;

public class TemplateTest {

	public static void main(String[] args) throws Exception {
		
		//准备数据
		Map map = new HashMap();
		map.put("url", "yesc.com.cn");
		map.put("name", "管理员");
		
		List list = new ArrayList();
		list.add("1");
		list.add("2");
		
		Map context = new HashMap();
		context.put("user", "longzhun");
		context.put("age", new Long(25000));
		context.put("date", new Date());
		context.put("numberList", list);
		context.put("map", map);
		
		TemplateEngine freemarkerEngine = (TemplateEngine) TemplateFactory.getInstance().getBean("freemarker");
		freemarkerEngine.run(context);
		
		TemplateEngine volecityEngine = (TemplateEngine) TemplateFactory.getInstance().getBean("velocity");
		volecityEngine.run(context);
	}
}

 

 最后奉上文档:

 

你可能感兴趣的:(java,apache,freemarker,jms,velocity)