时间:2023-06-12 09:45:01 | 来源:网站运营
时间:2023-06-12 09:45:01 来源:网站运营
Java的FreeMarker模板引擎的几种模板加载方式:Configuration configuration = new Configuration(Configuration.getVersion());Template template;Map data = new LinkedHashMap();StringWriter stringWriter = new StringWriter();configuration.setDirectoryForTemplateLoading(new File(System.getProperty("user.dir") + "/src/main/resources/template"));template = configuration.getTemplate("test.ftl");
Configuration configuration = new Configuration(Configuration.getVersion());StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();Template template;Map data = new LinkedHashMap();StringWriter stringWriter = new StringWriter();stringTemplateLoader.putTemplate("test.ftl", "<meta http-equiv=/"refresh/" content=/"0; url=${url}/" />" + "<script>" + "document.location='${url}';" + "</script>");configuration.setTemplateLoader(stringTemplateLoader);template = configuration.getTemplate("test.ftl");data.put("url", "https://tongfu.net/");template.process(data, stringWriter);return stringWriter.toString();
Configuration configuration = new Configuration(Configuration.getVersion());ClassTemplateLoader classTemplateLoader = new ClassTemplateLoader(DemoController.class, "template/");Template template;Map data = new LinkedHashMap();StringWriter stringWriter = new StringWriter();configuration.setTemplateLoader(classTemplateLoader);template = configuration.getTemplate("test.ftl");data.put("url", "https://tongfu.net/");template.process(data, stringWriter);return stringWriter.toString();
不过在SpringBoot项目里面这样的设计似乎不能正常运行,只能放到resources目录下面,然后通过在路径前面增加“/”指明从resources目录开始查找,这样也就和指定什么类没有关系了。Configuration configuration = new Configuration(Configuration.getVersion());ClassTemplateLoader classTemplateLoader = new ClassTemplateLoader(DemoController.class, "/template/");Template template;Map data = new LinkedHashMap();StringWriter stringWriter = new StringWriter();configuration.setTemplateLoader(classTemplateLoader);template = configuration.getTemplate("test.ftl");data.put("url", "https://tongfu.net/");template.process(data, stringWriter);return stringWriter.toString();
Configuration configuration = new Configuration(Configuration.getVersion());FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(new File(System.getProperty("user.dir") + "/src/main/resources/template/"));Template template;Map data = new LinkedHashMap();StringWriter stringWriter = new StringWriter();configuration.setTemplateLoader(fileTemplateLoader);template = configuration.getTemplate("test.ftl");data.put("url", "https://tongfu.net/");template.process(data, stringWriter);return stringWriter.toString();
Configuration configuration = new Configuration(Configuration.getVersion());MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(new TemplateLoader[]{ new FileTemplateLoader(new File(System.getProperty("user.dir") + "/src/main/resources/abc/")), new FileTemplateLoader(new File(System.getProperty("user.dir") + "/src/main/resources/template/"))});Template template;Map data = new LinkedHashMap();StringWriter stringWriter = new StringWriter();configuration.setTemplateLoader(multiTemplateLoader);template = configuration.getTemplate("test.ftl");data.put("url", "https://tongfu.net/");template.process(data, stringWriter);return stringWriter.toString();
关键词:模板,方式,引擎