版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)
1.pom.xml中配置
<spark-template.version>2.7.1</spark-template.version>
<!-- freemarker模板-->
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-freemarker</artifactId>
<version>${spark-template.version}</version>
</dependency>
<!-- velocity模板-->
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-velocity</artifactId>
<version>${spark-template.version}</version>
</dependency>
<!-- jade模板-->
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-jade</artifactId>
<version>${spark-template.version}</version>
</dependency>
2.项目结构
3.controller写法
@Override
public void router() {
// 测试velocity
get("/oaPerf", (req, res) -> {
String context = req.contextPath();
Map<String, Object> model = new HashMap<>();
model.put("projectPath", context == null ? "123" : context);
return new VelocityTemplateEngine().render(new ModelAndView(model, "templates/oaPerf.vm"));
});
// 测试freemarker
get("/testFtl", (req, res) -> {
String context = req.contextPath();
Map<String, Object> model = new HashMap<>();
model.put("projectPath", context == null ? "213" : context);
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(this.getClass(), "/templates/");
return new FreeMarkerEngine(cfg).render(new ModelAndView(model, "test.ftl"));
});
//测试jade
get("/bankView", (req, res) -> {
String context = req.contextPath();
Map<String, Object> model = new HashMap<String, Object>();
model.put("projectPath", context == null ? "321" : context);
return new JadeTemplateEngine().render(new ModelAndView(model, "bankManage"));
});
}
看源码大致是三个模板的资源加载器默认路径设置的不同,后续有时间研究。。。