js模板(java调用js),邮件模板引擎使用
https://www.iteye.com/blog/user/yuhuiblog6956676dfdsfs8883568269/blog/2509258
以上是string模板,html模板也有对应的模板框架
用对应的引擎把模板路径,实际内容传入即可--思路一样
1,对应的模板引擎加入模板路径生成对应的模板实例
2,模板实例处理要处理的内容
davinci的邮件模板源码:
MailUtils
inviteOrgMemberTemplate.html
String text = templateEngine.process(template, context);
public final String process(String template, IContext context) {
return this.process(new TemplateSpec(template, (Set)null, (TemplateMode)null, (String)null, (Map)null), context);
}
public final String process(TemplateSpec templateSpec, IContext context) {
FastStringWriter stringWriter = new FastStringWriter(100);
this.process((TemplateSpec)templateSpec, (IContext)context, (Writer)stringWriter);
return stringWriter.toString();
}
davinci的脚本模板源码:
ScriptUtiils
executeParam.js
Constants
public static ScriptEngine getExecuptParamScriptEngine() throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new InputStreamReader(classLoader.getResourceAsStream(Constants.EXECUTE_PARAM_FORMAT_JS)));---模板路径
return engine;
}
WidgetServiceImpl:
ScriptEngine engine = getExecuptParamScriptEngine();
executeParam = getViewExecuteParam((engine), null, widget.getConfig(), null);--通过模板引擎调用js(java调用js)
配置解析成json对象:
function getDashboardItemExecuteParam(dashboardConfigJson, widgetConfigJson, itemId) {
var dashboardConfig = JSON.parse(dashboardConfigJson || '{}')
var widgetConfig = JSON.parse(widgetConfigJson || '{}')
var globalFilters = getGlobalFilters(dashboardConfig, +itemId)
var widgetExecuteParam = getWidgetExecuteParam(widgetConfig)
widgetExecuteParam.filters = widgetExecuteParam.filters.concat(globalFilters.filters)
widgetExecuteParam.filters = widgetExecuteParam.filters.map(function(filter) { return JSON.stringify(filter) })
widgetExecuteParam.params = widgetExecuteParam.params.concat(globalFilters.params)
return widgetExecuteParam
}
function getWidgetExecuteParam(widgetConfig) {
var _widgetConfig = widgetConfig;
var cols = _widgetConfig.cols;
var rows = _widgetConfig.rows;
var metrics = _widgetConfig.metrics;
var filters = _widgetConfig.filters;
var color = _widgetConfig.color;
var label = _widgetConfig.label;
var size = _widgetConfig.size;
var xAxis = _widgetConfig.xAxis;
var tip = _widgetConfig.tip;
var orders = _widgetConfig.orders;
var cache = _widgetConfig.cache;
var expired = _widgetConfig.expired;
var nativeQuery = false
if (_widgetConfig.chartStyles && _widgetConfig.chartStyles.table) {
nativeQuery = _widgetConfig.chartStyles.table.withNoAggregators
}
....
}
public static ViewExecuteParam getViewExecuteParam(ScriptEngine engine, String dashboardConfig, String widgetConfig, Long releationId) {
try {
Invocable invocable = (Invocable) engine;
Object obj = invocable.invokeFunction("getDashboardItemExecuteParam", dashboardConfig, widgetConfig, releationId);
if (obj instanceof ScriptObjectMirror) {
ScriptObjectMirror vsom = (ScriptObjectMirror) obj;
List<String> groups = new ArrayList<>();
List<Aggregator> aggregators = new ArrayList<>();
List<Order> orders = new ArrayList<>();
List<String> filters = new ArrayList<>();
Boolean cache = false;
Boolean nativeQuery = false;
Boolean filterAndFlag = false;
Long expired = 0L;
List<Param> params = new ArrayList<>();
for (String key : vsom.keySet()) {
switch (key) {
case "groups":
ScriptObjectMirror groupMirror = (ScriptObjectMirror) vsom.get(key);
if (groupMirror.isArray()) {
Collection<Object> values = groupMirror.values();
values.forEach(v -> groups.add(String.valueOf(v)));
}
break;
case "aggregators":
ScriptObjectMirror aggregatorsMirror = (ScriptObjectMirror) vsom.get(key);
if (aggregatorsMirror.isArray()) {
Collection<Object> values = aggregatorsMirror.values();
values.forEach(v -> {
ScriptObjectMirror agg = (ScriptObjectMirror) v;
Aggregator aggregator = new Aggregator(String.valueOf(agg.get("column")), String.valueOf(agg.get("func")));
aggregators.add(aggregator);
});
}
break;
case "orders":
ScriptObjectMirror ordersMirror = (ScriptObjectMirror) vsom.get(key);
if (ordersMirror.isArray()) {
Collection<Object> values = ordersMirror.values();
values.forEach(v -> {
ScriptObjectMirror agg = (ScriptObjectMirror) v;
Order order = new Order(String.valueOf(agg.get("column")), String.valueOf(agg.get("direction")));
orders.add(order);
});
}
break;
case "filters":
Object o = vsom.get(key);
if (o instanceof String) {
String filtersJsonStr = (String) o;
if (!StringUtils.isEmpty(filtersJsonStr)) {
filters.add(filtersJsonStr);
}
} else if (o instanceof ScriptObjectMirror) {
ScriptObjectMirror filterMirror = (ScriptObjectMirror) o;
if (filterMirror.isArray() && filterMirror.size() > 0) {
Collection<Object> values = filterMirror.values();
values.forEach(v -> {
filters.add(String.valueOf(v));
});
}
}
break;
case "cache":
cache = (Boolean) vsom.get(key);
break;
case "expired":
expired = Long.parseLong(String.valueOf(vsom.get(key)));
break;
case "params":
ScriptObjectMirror paramsMirror = (ScriptObjectMirror) vsom.get(key);
if (paramsMirror.isArray()) {
Collection<Object> values = paramsMirror.values();
values.forEach(v -> {
ScriptObjectMirror agg = (ScriptObjectMirror) v;
Param param = new Param(String.valueOf(agg.get("name")), String.valueOf(agg.get("value")));
params.add(param);
});
}
break;
case "nativeQuery":
nativeQuery = (Boolean) vsom.get(key);
break;
case "filterAndFlag":
filterAndFlag = (Boolean) vsom.get(key);
break;
}
}
return new ViewExecuteParam(groups, aggregators, orders, filters, params, cache, expired, nativeQuery,filterAndFlag);
}
} catch (ScriptException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
上一篇: Vue引用外部样式文件步骤详解
下一篇: js存储键值实例详解