beetl模板入门例子

加入maven依赖

<dependency>
	<groupId>org.beetl</groupId>
	<artifactId>beetl-core</artifactId>
	<version>2.2.3</version>
</dependency>

模板文件 src/main/resources/template/hello.btl 的内容为:

hello ${name}
你好:${name}


例子代码如下:

package com.lala.template;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.FileResourceLoader;
import org.beetl.core.resource.StringTemplateResourceLoader;

public class Beetl
{
	/**
	 * 加载classpath下的资源
	 * @throws Exception
	 */
	static void classpathRL()throws Exception
	{
		ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();		
		Configuration cfg = Configuration.defaultConfiguration();
		GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
		Template t = gt.getTemplate("/template/hello.btl");	
		t.binding("name", "beetl");		
		String str = t.render();
		System.out.println(str);
	}
	
	/**
	 * 加载文件系统下的资源
	 * @throws Exception
	 */
	static void filePathRL()throws Exception
	{
		String root = "E:\\pp\\src\\camel-web\\src\\main\\resources\\template";
		FileResourceLoader resourceLoader = new FileResourceLoader(root,"utf-8");		
		Configuration cfg = Configuration.defaultConfiguration();
		GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
		Template t = gt.getTemplate("hello.btl");	
		t.binding("name", "beetl");		
		String str = t.render();		
		System.out.println(str);
	}
	
	/**
	 * 直接构建字符串
	 * @throws Exception
	 */
	static void stringBeetl()throws Exception
	{
		StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();		
		Configuration cfg = Configuration.defaultConfiguration();
		GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
		Template t = gt.getTemplate("hello,${name} \r\n你好:${name}");
		t.binding("name", "beetl");		
		String str = t.render();
		System.out.println(str);
	}
}

servlet例子如下:

package com.lala.template;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.WebAppResourceLoader;

@WebServlet("/beetl")
public class BeetlServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		WebAppResourceLoader  resourceLoader = new WebAppResourceLoader(this.getServletContext().getRealPath("/"), "UTF-8");		
		Configuration cfg = Configuration.defaultConfiguration();
		GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
		Template t = gt.getTemplate("/template/webhello.btl");	
		t.binding("name", "beetl");		
		t.renderTo(resp.getWriter());
	}
}


你可能感兴趣的:(beetl)