参考 com.jfinal.render.TemplateRender 写一个 ThymeleafRender 出来: public class Thymeleaf extends Render { ... }
然后创建一个 MyRenderFactory: public class MyRenderFactory extends RenderFactory { public Render getRender(String view) { return new ThymeleafRender(view); } }
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(JFinal.me().getServletContext());
// XHTML is the default mode, but we will set it anyway for better understanding of code templateResolver.setTemplateMode(templateMode); templateResolver.setPrefix(prefix); templateResolver.setSuffix(suffix); //templateResolver.setCharacterEncoding("UTF-8"); templateResolver.setCharacterEncoding(encoding); // Set template cache TTL to 1 hour. If not set, entries would live in cache until expelled by LRU templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
templateResolver.setCacheable(!me.getDevMode());
templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver);
public class Thymeleaf extends Render {
...
}
然后创建一个 MyRenderFactory:
public class MyRenderFactory extends RenderFactory {
public Render getRender(String view) {
return new ThymeleafRender(view);
}
}
最后再 configConstant(Constants me) 中配置一下即可:
me.setRenderFactory(new MyRenderFactory());
用的时候这样:
render(view);
除了前面讲的可以参考 TemplateRender 以外,还可以参考 com.jfinal.render 包下面的很多其它 Render,例如 FreemarkerRender ,都很简单,三四十行代码的事
最后,强列建议直接使用 jfinal 默认的模板引擎,无论是易用性、学习成本还是性能,甩 Thymeleaf 五十条街很轻松
在这里有文档,10 分钟掌握 90% 的用法:
http://www.jfinal.com/doc/2-1