use of org.beetl.core.resource.FileResourceLoader in project nutzboot by nutzam.
the class BeetlGroupTemplateStarter method getGroupTemplate.
@IocBean(depose = "close")
public GroupTemplate getGroupTemplate() throws IOException {
Properties prop = new Properties();
for (String key : conf.keySet()) {
if (key.startsWith("beetl.")) {
prop.put(key.substring("beetl.".length()), conf.get(key));
}
}
System.out.println(prop);
if (!prop.containsKey(Configuration.RESOURCE_LOADER)) {
prop.put(Configuration.RESOURCE_LOADER, ClasspathResourceLoader.class.getName());
}
if (!prop.containsKey("RESOURCE.autoCheck")) {
prop.put("RESOURCE.autoCheck", "true");
}
if (!prop.containsKey("RESOURCE.root")) {
prop.put("RESOURCE.root", prop.getProperty("root", "template/"));
}
if (!prop.containsKey(Configuration.DIRECT_BYTE_OUTPUT)) {
// 默认启用DIRECT_BYTE_OUTPUT,除非用户自定义, 一般不会.
log.debug("no custom DIRECT_BYTE_OUTPUT found , set to true");
// 当DIRECT_BYTE_OUTPUT为真时, beetl渲染会通过getOutputStream获取输出流
// 而BeetlView会使用LazyResponseWrapper代理getOutputStream方法
// 从而实现在模板输出之前,避免真正调用getOutputStream
// 这样@Fail视图就能正常工作了
prop.put(Configuration.DIRECT_BYTE_OUTPUT, "true");
}
if (!prop.containsKey(Configuration.ERROR_HANDLER)) {
// 没有自定义ERROR_HANDLER,用定制的
prop.put(Configuration.ERROR_HANDLER, LogErrorHandler.class.getName());
}
Configuration cfg = new Configuration(prop);
String local = conf.get(PROP_TEMPLATE_ROOT_LOCAL);
GroupTemplate gt = new GroupTemplate(cfg);
if (!Strings.isBlank(local)) {
try {
if (new File(local).exists()) {
local = new File(local).getAbsolutePath();
FileResourceLoader resourceLoader = new FileResourceLoader(local);
resourceLoader.setAutoCheck(true);
gt.setResourceLoader(resourceLoader);
log.debugf("Template Local path=%s is ok, use it", local);
}
} catch (Throwable e) {
log.infof("Template Local path=%s is not vaild, fallback to beetl.RESOURCE.root", local, e);
}
}
return gt;
}
use of org.beetl.core.resource.FileResourceLoader in project beetl2.0 by javamonkey.
the class ResourceLoaderTest method testSimple.
@Test
public void testSimple() throws Exception {
FileResourceLoader f = new FileResourceLoader();
boolean exist = f.exist("/build.xml");
AssertJUnit.assertTrue(exist);
ClasspathResourceLoader cp = new ClasspathResourceLoader();
exist = cp.exist("/template/resourceloader/cp.txt");
AssertJUnit.assertTrue(exist);
}
use of org.beetl.core.resource.FileResourceLoader in project beetl2.0 by javamonkey.
the class CompositeResourceLoaderTest method testSimple.
@Test
public void testSimple() throws Exception {
Configuration conf = Configuration.defaultConfiguration();
CompositeResourceLoader loader = new CompositeResourceLoader();
String home = System.getProperty("user.dir");
String path1 = home + "/src/test/resources/template/resourceloader/var1";
String path2 = home + "/src/test/resources/template/resourceloader/var2";
FileResourceLoader fileLoader1 = new FileResourceLoader(path1);
FileResourceLoader fileLoader2 = new FileResourceLoader(path2);
Map data = getData();
// 根据id加载
MapResourceLoader mapLoader = new MapResourceLoader(data);
loader.addResourceLoader(new StartsWithMatcher("http:").withoutPrefix(), fileLoader2);
loader.addResourceLoader(new StartsWithMatcher("db:").withoutPrefix(), mapLoader);
loader.addResourceLoader(new AllowAllMatcher(), fileLoader1);
GroupTemplate gt = new GroupTemplate(loader, conf);
Template t = gt.getTemplate("/xxx.html");
t.binding("a", "hello");
String result = t.render();
;
AssertJUnit.assertEquals("hellohello--file2:hello--db=hello", result);
}
use of org.beetl.core.resource.FileResourceLoader in project beetl2.0 by javamonkey.
the class GroupTemplateTest method main.
public static void main(String[] args) throws Exception {
String home = System.getProperty("user.dir") + File.separator + "template" + File.separator;
Configuration cf = Configuration.defaultConfiguration();
cf.setStatementStart("<!--:");
cf.setStatementEnd("-->");
FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
GroupTemplate gt = new GroupTemplate(rs, cf);
List<StockModel> list = StockModel.dummyItems();
Template t = gt.getTemplate("/helloworld.html");
t.binding("items", list);
StringWriter sw = new StringWriter();
t.renderTo(sw);
System.out.println(sw.toString());
// 第二次
t = gt.getTemplate("/helloworld.html");
t.binding("items", list);
sw = new StringWriter();
t.renderTo(sw);
System.out.println(sw.toString());
}
Aggregations