我通过java代码调用beetl模板文件,代码如下:
public static Template getTemplate(String data,String type) {
GroupTemplate gt=null;
String root ="";
if(Strings.isBlank(data)){
type="html";
data="\"hello,${name!} \\r\\n你好:${name!}\"";
}
if(Strings.isBlank(type)){
type="html";
}
try {
Configuration cfg =Configuration.defaultConfiguration();
switch (type) {
case "classpath":
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
gt = new GroupTemplate(resourceLoader, cfg);
break;
case "user":
root = getPath(true);
case "beetl":
FileResourceLoader fileLoader = new FileResourceLoader(BeetlUtil.getWebRoot(),"utf-8");
gt = new GroupTemplate(fileLoader, cfg);
break;
case "html":
StringTemplateResourceLoader stringLoader = new StringTemplateResourceLoader();
gt = new GroupTemplate(stringLoader, cfg);
break;
}
} catch (IOException e) {
System.out.println(e);
}
return gt.getTemplate(data);
}
调用为:
Template template= Utils.getTemplate("/pages/user/company.html","beetl");
这个模板文件所在项目位置:webapp/WEB-INF/views/pages/user/company.html
webapp/WEB-INF/views/ 为beetl根目录。
非打包模式下运行正常无误。
打包后运行出现如下错误:
[DEBUG] 2018-05-10 22:21:28,133 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - SELECT * FROM sys_menu WHERE href IN (?,?) ORDER BY href DESC, path DESC
| 1 | 2 |
|-----------|----------------------------------------------|
| /template | /template/beetl?url=/pages/user/company.html |
For example:> "SELECT * FROM sys_menu WHERE href IN ('/template','/template/beetl?url=/pages/user/company.html') ORDER BY href DESC, path DESC "
>>10:21:28:错找不到指定模板或者加载错(TEMPLATE_LOAD_ERROR): 位于0行 资源:/pages/user/company.html 模板根目录为 C:\working\project\shu.epei.xin\dst\tmp\webinf\WEB-INF\views
FileResourceLoader,Root=C:\working\project\shu.epei.xin\dst\tmp\webinf\WEB-INF\views
[DEBUG] 2018-05-10 22:21:28,322 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [GET ]URI=/epei/template/beetl 1902ms
22:21:28.559 [QuartzScheduler_defaultScheduler-DESKTOP-EVPFAKR1525962067926_ClusterManager] DEBUG org.quartz.impl.jdbcjobstore.JobStoreTX - ClusterManager: Check-in complete.
22:21:36.240 [defau
请指教!