作为一个模板文件加载的抽象,自然不能限制模板来自何方,在FreeMarker中由几个主要的实现类来体现,这些TemplateLoader是可以独立使用的,Webapp需要Servlet环境。当然你可以实现自己的TemplateLoader.
内存字符串的使用。
@Test public void testStringTL() throws IOException { StringTemplateLoader stl = new StringTemplateLoader(); String template = "rrrrr${key}rrrrr"; stl.putTemplate("hello", template); Object source = stl.findTemplateSource("hello"); Reader reader = stl.getReader(source, "utf-8"); String dest = IOUtils.toString(reader); Assert.assertEquals(template, dest); }
public static void main(String[] args){
// 读取项目下的文件 Resource resource = new ClassPathResource("sqls/xxx.sql"); InputStream inputStream = null; String sql = null; try { inputStream = resource.getInputStream(); byte[] bytes = IOUtils.toByteArray(inputStream); sql = new String(bytes); sql = sql.replaceAll("\r\n\t"," "); sql = sql.replaceAll("\r\n"," "); sql = sql.replaceAll("\t",""); sql = sql.replaceAll("[ ]+"," ");
// 替换参数 Map<String,Object> paramMap = new HashMap<>(); paramMap.put("beginDate","20170901"); paramMap.put("futureEndDate","20170914"); paramMap.put("taskid","8a7a8282600731bb016007324d770000"); List<SqlListEntity> cycleList = new ArrayList<>(); SqlListEntity entity1 = new SqlListEntity(); entity1.setName("第1周"); cycleList.add(entity1); SqlListEntity entity2 = new SqlListEntity(); entity2.setName("第2周"); cycleList.add(entity2); paramMap.put("cycleList",cycleList); Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
// StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); stringTemplateLoader.putTemplate(sql,sql); configuration.setTemplateLoader(stringTemplateLoader); Template temp = configuration.getTemplate(sql); StringWriter queryStringWriter = new StringWriter(); temp.process(paramMap, queryStringWriter); System.out.println(queryStringWriter.toString()); } catch(IOException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(inputStream); } }
TemplateLoader是可以多种类型,同种类型组合起来使用的,查询顺序按照数组的顺序优先。
@Test public void testMultiTL() throws IOException { TemplateLoader ctl = new ClassTemplateLoader(TemplateLoaderTest.class, "/"); TemplateLoader ftl1 = new FileTemplateLoader(new File( System.getProperty("user.dir"))); MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] { ftl1,ctl }); Object source = mtl.findTemplateSource("test.ftl"); Reader reader = mtl.getReader(source, "utf-8"); String dest = IOUtils.toString(reader); Assert.assertEquals("${hello}", dest); }
@Test public void testInConfiguration() throws IOException { Configuration configuration = new Configuration( Configuration.VERSION_2_3_21); configuration.setDefaultEncoding("utf-8"); TemplateLoader ctl = new ClassTemplateLoader(TemplateLoaderTest.class, "/"); TemplateLoader ftl1 = new FileTemplateLoader(new File( System.getProperty("user.dir"))); MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] { ftl1,ctl }); configuration.setTemplateLoader(mtl); //configuration.getTemplate("test.ftl").process(data, out); }
模板加载通常是耗费资源的,默认是开启缓存的,缓存的实现,是否使用缓存取决于你
configuration.setCacheStorage(new freemarker.cache.NullCacheStorage()); configuration.clearTemplateCache();
java-Freemarker TemplateLoader实现模版
原文:http://www.cnblogs.com/hwaggLee/p/7927841.html