在日常开发中,我们有时候需要发送短信、邮件等通知,但是这些通知的内容通常都是动态的,而且可能会发生变动,为了程序的灵活性,我们通常会将通知的内容配置在页面上,然后后台通过渲染这些模板,来获取具体的内容。而 freemarker 正好可以帮助我们来完整模板的渲染这一步。

需求:
    1、给定一个字符串模板,渲染出内容
    2、修改这个字符串模板,然后再次渲染

实现要点:
    1、模板的加载器需要使用 StringTemplateLoader
    2、模板不可使用 Configuration.getTemplate,而应该使用 new Template

3、StringTemplateLoader 上的一段注释

 
完整代码如下:

@Test
public void test001() throws Exception {
String templateName = "hello-template";
String templateValue = "hello,${name}";
Configuration configuration = configuration();
processTemplate(configuration, templateName, templateValue);
// -------------------- 进行模板的修改 ------------------------
templateValue = "hello,${name},我今年,${age}岁.";
processTemplate(configuration, templateName, templateValue);
} /**
* 解析模板
*
* @param configuration
* @param templateName
* @throws IOException
* @throws TemplateException
*/
private void processTemplate(Configuration configuration, String templateName, String templateValue) throws IOException, TemplateException {
Map<String, Object> root = new HashMap<>(4);
root.put("name", "你好");
root.put("age", 25);
StringWriter stringWriter = new StringWriter();
Template template = new Template(templateName, templateValue, configuration);
template.process(root, stringWriter);
System.out.println(stringWriter.toString());
} /**
* 配置 freemarker configuration
*
* @return
*/
private Configuration configuration() {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
StringTemplateLoader templateLoader = new StringTemplateLoader();
configuration.setTemplateLoader(templateLoader);
configuration.setDefaultEncoding("UTF-8");
return configuration;
}

执行结果:

freemarker中使用String字符串作为模板的更多相关文章

  1. freemarker中的split字符串分割

    freemarker中的split字符串分割 1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串 ...

  2. freemarker中的split字符串分割(十六)

    1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串分割--> <#list &quo ...

  3. 判断集合中存在String字符串 或 判断集合中不存在String字符串

    一.使用场景 用于集合中有多个相近的字符,无法使用包含判断 如: 这里如果我想判断以上集合中是否包含"信封件-DE"就会被"信封件-DE2"影响到 毕竟:&qu ...

  4. Java中的String字符串及其常用方法

    字符串(String) 文章目录 字符串(String) 直接定义字符串 常用方法 字符串长度 toLowerCase() & toUpperCase()方法 trim()方法去除空格 判空 ...

  5. java中的string字符串中的trim函数的作用

    去掉字符串首尾空格 防止不必要的空格导致错误public class test{ public static void main(String[] args) { String str = " ...

  6. android中获取string字符串的方法

    比如在arrays.xml里: <!--leo added for KYLIN-496--> <string-array name="reboot_item"&g ...

  7. C++中去掉string字符串中的\r\n等

    string imagedata;imagedata = “dudau\r\ndadafca\r\n” CString Image; Image = imagedata.c_str(); Image. ...

  8. C实现string字符串

    在C中实现string字符串,使用typedef将string定义为char *. #include <stdio.h> #include <stdlib.h> #includ ...

  9. ES6, Angular,React和ABAP中的String Template(字符串模板)

    String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...

随机推荐

  1. EF架构封装类

    http://www.woxihuan.com/46528208/1323334777088641.shtml http://www.ediclot.com/archives/2368 http:// ...

  2. 加入Erlang社区-指引

    国内暂且没有发现较活跃.人气较高的论坛或者社区,偶然发现Erlang官网的Community页面描述了一个Slack交流平台,里面有众多异国他乡的大佬,感兴趣的.有技术疑问的都可以加入看看. 加入教程 ...

  3. 一文带你了解.Net读写锁

    本文主要讲解.Net基于ReaderWriterLockSlim讲解读写锁 基础概念 读写锁是一个具有特殊用途的线程锁,适用于频繁读取且读取需要一定时间的场景,共享资源的读取操作通常是可以同时执行的, ...

  4. 288 day05_异常,线程

    day05 [异常.线程] 主要内容 异常.线程 教学目标 [ ] 能够辨别程序中异常和错误的区别 [ ] 说出异常的分类 [ ] 说出虚拟机处理异常的方式 [ ] 列举出常见的三个运行期异常 [ ] ...

  5. PHP中的数据库连接持久化

    数据库的优化是我们做web开发的重中之重,甚至很多情况下其实我们是在面向数据库编程.当然,用户的一切操作.行为都是以数据的形式保存下来的.在这其中,数据库的连接创建过程有没有什么可以优化的内容呢?答案 ...

  6. Go学习【01】:初步学习需要的知识

    理解以下知识,初步写简单go项目就足够了 语言参考(基础篇) 基本语法 基本组成 包声明 引入包 函数 变量 语句 & 表达式 注释 其它(可忽略) go没有像php.js那样需要在文件开始申 ...

  7. sonar-scanner的使用

    在服务器搭建sonarqube后,本地的windows个人电脑如何使用sonar-scanner? 在服务器搭建sonarqube后,每个人都可以在本地使用sonar-scanner扫描代码. son ...

  8. python读取ini文件

    import configparser import os config=configparser.ConfigParser()#创建config对象 file_path=os.path.dirnam ...

  9. P3288-[SCOI2014]方伯伯运椰子【0/1分数规划,负环】

    正题 题目链接:https://www.luogu.com.cn/problem/P3288 题目大意 给出\(n\)个点\(m\)条边的一张图,没条边\(i\)流量为\(c_i\),费用是\(d_i ...

  10. 一篇文章告诉你Python接口自动化测试中读取Text,Excel,Yaml文件的方法

    前言 不管是做Ui自动化和接口自动,代码和数据要分离,会用到Text,Excel,Yaml.今天讲讲如何读取文件数据 Python也可以读取ini文件,传送门 记住一点:测试的数据是不能写死在代码里面 ...