JFinal如何集成Thymeleaf模版引擎

有没有大佬提示下,在项目中如果集成Thymeleaf   模板?


急,,求助!!

评论区

JFinal

2018-05-17 22:13

参考 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);
}
}

最后再 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

flyinkeke

2018-05-18 20:23

package cn.flyinke.jke.thymeleaf.common;

import java.io.PrintWriter;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import com.jfinal.config.Constants;
import com.jfinal.core.JFinal;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;


public class ThymeleafRender extends Render {

private static TemplateEngine templateEngine;
private static String encoding = "UTF-8";
private static String contentType = "text/html; charset=" + encoding;
private static String templateMode = "HTML5";
private static String prefix = "/";
private static String suffix = ".html";

public ThymeleafRender(String view) {
this.view = view;
}

public static void setTemplateMode(String templateMode) {
ThymeleafRender.templateMode = templateMode;
}

public static void setPrefix(String prefix) {
ThymeleafRender.prefix = prefix;
}

public void setSuffix(String suffix) {
ThymeleafRender.suffix = suffix;
}

public static void init() {
if (templateEngine != null) {
return;
}

Constants me = JFinal.me().getConstants();
encoding = me.getEncoding();
contentType = "text/html; charset=" + encoding;

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);

}

@Override
public void render() {
response.setContentType(contentType);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

WebContext context = new WebContext(request, response, request.getServletContext(), request.getLocale());
// for (Enumeration attrs=request.getAttributeNames(); attrs.hasMoreElements();) {
// String attrName = attrs.nextElement();
// context.setVariable(attrName, request.getAttribute(attrName));
// }

PrintWriter writer = null;
try {
writer = response.getWriter();
templateEngine.process(view, context, writer);
} catch (Exception e) {
throw new RenderException(e);
}
finally {
if (writer != null)
writer.close();
}
}
}

李若谷

2018-05-20 11:42

@flyinkeke 谢大神,稍后试一下,感谢感谢!!

李若谷

2018-05-20 11:48

@JFinal 波哥,谢了,这两天试了下,确实Jfinal自带的Enjoy 很强大。

JFinal

2018-05-21 09:06

@李若谷 不仅功能强大,学习成本极低,而且性能极高,这里有性能测试:
https://www.oschina.net/news/90815/jfinal-3-3

这里有专用于 enjoy 性能测试的项目,自己可以在本地与 Thymeleaf 再测试一下:
https://gitee.com/jfinal/enjoy-performance

热门反馈

扫码入社