java-Freemarker TemplateLoader實現模版

 

TemplateLoader的實現

做爲一個模板文件加載的抽象,天然不能限制模板來自何方,在FreeMarker中由幾個主要的實現類來體現,這些TemplateLoader是能夠獨立使用的,Webapp須要Servlet環境。固然你能夠實現本身的TemplateLoader.sql

  • 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("\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); } }

 

 

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);
}

 

其它

緩存

模板加載一般是耗費資源的,默認是開啓緩存的,緩存的實現,是否使用緩存取決於你app

configuration.setCacheStorage(new freemarker.cache.NullCacheStorage());

configuration.clearTemplateCache();
相關文章
相關標籤/搜索