freemarker中使用String字符串作为模板
在日常开发中,我们有时候需要发送短信、邮件等通知,但是这些通知的内容通常都是动态的,而且可能会发生变动,为了程序的灵活性,我们通常会将通知的内容配置在页面上,然后后台通过渲染这些模板,来获取具体的内容。而 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字符串作为模板的更多相关文章
- freemarker中的split字符串分割
freemarker中的split字符串分割 1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串 ...
- freemarker中的split字符串分割(十六)
1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串分割--> <#list &quo ...
- 判断集合中存在String字符串 或 判断集合中不存在String字符串
一.使用场景 用于集合中有多个相近的字符,无法使用包含判断 如: 这里如果我想判断以上集合中是否包含"信封件-DE"就会被"信封件-DE2"影响到 毕竟:&qu ...
- Java中的String字符串及其常用方法
字符串(String) 文章目录 字符串(String) 直接定义字符串 常用方法 字符串长度 toLowerCase() & toUpperCase()方法 trim()方法去除空格 判空 ...
- java中的string字符串中的trim函数的作用
去掉字符串首尾空格 防止不必要的空格导致错误public class test{ public static void main(String[] args) { String str = " ...
- android中获取string字符串的方法
比如在arrays.xml里: <!--leo added for KYLIN-496--> <string-array name="reboot_item"&g ...
- C++中去掉string字符串中的\r\n等
string imagedata;imagedata = “dudau\r\ndadafca\r\n” CString Image; Image = imagedata.c_str(); Image. ...
- C实现string字符串
在C中实现string字符串,使用typedef将string定义为char *. #include <stdio.h> #include <stdlib.h> #includ ...
- ES6, Angular,React和ABAP中的String Template(字符串模板)
String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...
随机推荐
- VMware安装IPFire防火墙镜像
之后便可以通过WEB登录到管理页面(admin账号,密码是在上面配置的) 详细可参考:https://www.mobibrw.com/2016/4900
- Docker安装Nginx(含:Windows启动、重启、停止)
Docker安装Nginx #docker pull nginx:latest (第一次启动Docker-Nginx) #docker run --detach \ --publish 80:80 \ ...
- Spring Boot 2.x 之 Spring Data JPA, Hibernate 5
1. Spring Boot常用配置项 基于Spring Boot 2.0.6.RELEASE 1.1 配置属性类 spring.jpa前缀的相关配置项定义在JpaProperties类中, 1.2 ...
- 剑指offer计划20( 搜索与回溯算法中等)---java
1.1.题目1 剑指 Offer 07. 重建二叉树 1.2.解法 注释解法. 1.3.代码 class Solution { int[] preorder; HashMap<Integer, ...
- 为什么不推荐Python初学者直接看项目源码
无论是有没有其他语言的经验,入门Python都很简单.Python拥有简单直观的语法,方便的语法糖,以及丰富的第三方库.只要一个基础的Python教程,大家基本上都能无障碍的入门.在入门之后,很多人对 ...
- swiper-wrapper轮滑组件(多组轮滑界面)间隔无效问题
在多组此种轮滑效果出现时,你需要加两个属性值,即 new Swiper('.swiper-container', { slidesPerView: 3, slidesPerColumn: 2, spa ...
- linux中如何查看文件上下文
grep -C 10 keyword catalina.out filename https://blog.csdn.net/weixin_34791683/article/details/11660 ...
- php设计模式--生成器模式
生成器模式 require "D:\\xxx\bild.php"; require "D:\\xxx\cx_bild.php"; require "D ...
- css颜色字符串转换, 字符串转化为驼峰形式
* 将 rgb 颜色字符串转换为十六进制的形式,如 rgb(255, 255, 255) 转为 #ffffff1. rgb 中每个 , 后面的空格数量不固定2. 十六进制表达式使用六位小写字母3. 如 ...
- git 报错 gitThere is no tracking information for the current branch. Please specify which branch you w
新建本地分支后将本地分支推送到远程库, 使用git pull 或者 git push 的时候报错gitThere is no tracking information for the current ...