TemplateLoader的实现
作为一个模板文件加载的抽象,自然不能限制模板来自何方,在FreeMarker中由几个主要的实现类来体现,这些TemplateLoader是可以独立使用的,Webapp需要Servlet环境。当然你可以实现自己的TemplateLoader.
- StringTemplateLoader 直接将内存中的String对象放入并使用
- FileTemplateLoader 本地文件目录
- ClassTemplateLoader ClassPath 加载
- WebappTemplateLoader ServletContext
- MultiTemplateLoader 多个TemplateLoader的叠加,顺序按照数组的顺序优先加载
StringTemplateLoader
内存字符串的使用。
@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(" "," "); sql = sql.replaceAll(" "," "); sql = sql.replaceAll(" ",""); 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); } }
MultiTemplateLoader
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); }
通常在Configuration中使用,才能方便的处理FreeMarker的表达式
@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();