是一款模板引擎,用来将html和后端数据结合在一块儿使用的通用类库。实际上是一个java类库。html
html模板 + 后端数据 = 输出java
经常使用类型:git
<#if condition>
...
<#elseif condition2>
...
<#else>
...
</#if> <!--必定要加的-->
...
复制代码
<#if x == 0>
...
<!-- 用gt表明大于 -->
<#elseif x gt 0 >
...
<!-- 用gte 表明小于 -->
<#elseif x gte 0>
...
复制代码
<!-- 判断数组是否等空 -->
<#if list?? && (list?size > 0)>
...
</#if>
<!-- 循环数组 -->
<#list categoryList as categorys>
复制代码
建立Configuration实例 + 数据模板(Map)+ 获取模板 + 合并github
try {
//1. 建立一个合适的Configration对象
Configuration cfg = new Configuration();
cfg.setDefaultEncoding("UTF-8"); //必须
// 2. 数据模板
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("list", categoryList);
paramMap.put("date", LocalDateTime.now().toLocalDate());
// 获取模板
....
// 合并,不须要输出时,注掉
//Writer writer = new OutputStreamWriter(new FileOutputStream("success.ftl"), "UTF-8");
//template.process(paramMap, writer);
// 将html输出
String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, paramMap);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
复制代码
模板加载器的方式:web
本地模板路径:src/main/webapp/WEB-INF/template/model.ftlspring
// 把磁盘的路径来撸下来,若是要上测试,线上环境就不兼容了
configuration.setDirectoryForTemplateLoading(new File(
C:\Users\xxx\Desktop\project\src\main\webapp\WEB-INF\template));
Template template = configuration.getTemplate("model.ftl");
复制代码
本地模板路径:src\main\resources\template\model.ftl this.class 路径:src\main\java\com\xxx\xxx\xxx\xxx\util\Util.java后端
configuration.setClassForTemplateLoading(this.getClass(), "/template");
Template template = configuration.getTemplate("model.ftl");
复制代码
基路径:Web应用根路径(WEB-INF目录的上级目录)的相对路径 加载应用上下文数组
// 1. 依赖注入serverContext
@AutoWried
ServerContext serverContext;
// 2. 利用webApplication
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext context = webApplicationContext.getServletContext();
复制代码
本地模板路径:src/main/webapp/WEB-INF/template/model.ftl安全
//默认的路径就在WEB-INF
configuration.setClassForTemplateLoading(serverContext, "/template");
Template template = configuration.getTemplate("model.ftl");
复制代码
RemoteTemplateLoader.javaapp
public class RemoteTemplateLoader extends URLTemplateLoader {
// 远程模板文件的存储路径(目录)
private String remotePath;
private static String yanxuanMailTemplate = "http://";
public RemoteTemplateLoader(String remotePath) {
if (remotePath == null) {
throw new IllegalArgumentException("remotePath is null");
}
this.remotePath = canonicalizePrefix(remotePath);
if (this.remotePath.indexOf('/') == 0) {
this.remotePath = this.remotePath.substring(this.remotePath.indexOf('/') + 1);
}
}
@Override
protected URL getURL(String name) {
name = name.replace("_zh_CN", ""); //不知道为啥生成的总会有这个,因此replace掉了
String fullPath = this.remotePath + name;
System.out.println(fullPath);
if ((this.remotePath.equals("/")) && (!isSchemeless(fullPath))) {
return null;
}
URL url = null;
try {
url = new URL(fullPath);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return url;
}
private static boolean isSchemeless(String fullPath) {
int i = 0;
int ln = fullPath.length();
if ((i < ln) && (fullPath.charAt(i) == '/')) {
i++;
}
while (i < ln) {
char c = fullPath.charAt(i);
if (c == '/') {
return true;
}
if (c == ':') {
return false;
}
i++;
}
return true;
}
}
复制代码
//建立Configration对象
Configuration cfg = new Configuration();
//建立一个获取外部url的
RemoteTemplateLoader remoteTemplateLoader = new RemoteTemplateLoader(yanxuanMailTemplate);
cfg.setTemplateLoader(remoteTemplateLoader);
Template template = cfg.getTemplate("/model.ftl");
复制代码
利用freemarker模板发送邮件:https://github.com/yangshuting1/spring-send-mail