java-Freemarker TemplateLoader实现模版
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("\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); }
其它
缓存
模板加载通常是耗费资源的,默认是开启缓存的,缓存的实现,是否使用缓存取决于你
configuration.setCacheStorage(new freemarker.cache.NullCacheStorage()); configuration.clearTemplateCache();
知识只有共享才能传播,才能推崇出新的知识,才能学到更多,这里写的每一篇文字/博客,基本都是从网上查询了一下资料然后记录下来,也有些是原滋原味搬了过来,也有时加了一些自己的想法
标签:
Freemarker
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架