-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderUtil.java
More file actions
99 lines (86 loc) · 3.4 KB
/
RenderUtil.java
File metadata and controls
99 lines (86 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.arloor.forwardproxy.util;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import java.util.Map;
public class RenderUtil {
private final static TemplateEngine textEngine = new TemplateEngine();
private final static TemplateEngine htmlEngine = new TemplateEngine();
static {
StringTemplateResolver textResolver = new StringTemplateResolver();
textResolver.setOrder(1);
textResolver.setTemplateMode(TemplateMode.TEXT);
// TODO Cacheable or Not ?
textResolver.setCacheable(true);
textEngine.setTemplateResolver(textResolver);
StringTemplateResolver templateResolver = new StringTemplateResolver();
templateResolver.setOrder(1);
templateResolver.setTemplateMode(TemplateMode.HTML);
// TODO Cacheable or Not ?
templateResolver.setCacheable(true);
htmlEngine.setTemplateResolver(templateResolver);
}
/**
* 使用 Thymeleaf 渲染 Text模版
* Text模版语法见:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#textual-syntax
*
* @param template 模版
* @param params 参数
* @return 渲染后的Text
*/
public static String text(String template, Map<String, Object> params) {
Context context = new Context();
context.setVariables(params);
return textEngine.process(template, context);
}
/**
* 使用 Thymeleaf 渲染 Html模版
*
* @param template Html模版
* @param params 参数
* @return 渲染后的html
*/
public static String html(String template, Map<String, Object> params) {
Context context = new Context();
context.setVariables(params);
return htmlEngine.process(template, context);
}
/**
* 测试用,展示如何使用
*
* @param args
*/
public static void main(String[] args) {
// 渲染String
String string_template = "这是[(${name.toString()})]"; // 直接name其实就行了,这里就是展示能调用java对象的方法
String value = RenderUtil.text(string_template, ImmutableMap.of("name", "ARLOOR"));
System.out.println(value);
// 渲染List
/**
* [# th:each="item : ${items}"]
* - [(${item})]
* [/]
*/
String list_template = "[# th:each=\"item : ${items}\"]\n" +
" - [(${item})]\n" +
"[/]";
String value1 = RenderUtil.text(list_template, ImmutableMap.of("items", Lists.newArrayList("第一个", "第二个")));
System.out.println(value1);
// 渲染Map
/**
* [# th:each="key : ${map.keySet()}"]
* - [(${map.get(key)})]
* [/]
*/
String map_template = "[# th:each=\"key : ${map.keySet()}\"]\n" +
" 这是 - [(${map.get(key)})]\n" +
"[/]";
String value2 = RenderUtil.text(map_template, ImmutableMap.of("map", ImmutableMap.of("a", "甲", "b", "乙")));
System.out.println(value2);
String html_template = "这是<span th:text=\"${name}\"></span>";
System.out.println(RenderUtil.html(html_template, ImmutableMap.of("name", "ARLOOR")));
}
}