beetl模板入門例子
阿新 • • 發佈:2017-06-22
2.2.3 source cti req pro strong odin sdn com 你好:${name}
加入maven依賴
[html] view plain copy
- <dependency>
- <groupId>org.beetl</groupId>
- <artifactId>beetl-core</artifactId>
- <version>2.2.3</version>
- </dependency>
模板文件 src/main/resources/template/hello.btl 的內容為:
hello ${name}
例子代碼如下:
[java] view plain copy
- 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例子如下:
[java] view plain copy
- 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模板入門例子