首页 > 代码库 > beetl模板入门例子
beetl模板入门例子
加入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}
你好:${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模板入门例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。