4.8. JFinal3.0&JFinal2.o集成方案

Beetl提供 JFinal3.0 集成,使用JFinal3BeetlRenderFactory ,通过如下注册即可使用beetl模板引擎

  1. public class DemoConfig extends JFinalConfig {
  2. public void configConstant(Constants me) {
  3. PropKit.use("a_little_config.txt"); // 加载少量必要配置,随后可用PropKit.get(...)获取值
  4. me.setDevMode(PropKit.getBoolean("devMode", false));
  5. JFinal3BeetlRenderFactory rf = new JFinal3BeetlRenderFactory();
  6. rf.config();
  7. me.setRenderFactory(rf);
  8. GroupTemplate gt = rf.groupTemplate;
  9. //根据gt可以添加扩展函数,格式化函数,共享变量等,
  10. }

业务逻辑代码:

  1. public void modify(){
  2. int artId = getParaToInt(0, -1);
  3. setAttr("title", "修改文章");
  4. List<Cate> cateLists = Cate.getAllCate();
  5. //模板里访问cateLists,atr,
  6. setAttr("cateLists", cateLists);
  7. setAttr("art", Article.dao.findById(artId));
  8. render("/modify.html");
  9. }

BeetlRenderFactory 默认使用FileResourceLoader ,其根目录位于WebRoot目录下,如果你需要修改到别的目录,可以设置配置文件,如

  1. RESOURCE.root= /WEB-INF/template/

https://git.oschina.net/xiandafu/beetl-jfinal-sample 有完整例子,采用jfinal+beetl写的一个博客系统

https://git.oschina.net/xiandafu/jfinal_beet_beetsql_btjson 同上,但DAO部分采用了BeetlSql

JFinal3 与 Jfinal2.0不兼容,且不像Appache Common Lang那样,不兼容情况下采用不同的包名,导致了Beetl不能同时兼容:如果想在beetl(2.7.10)以后版本中仍然使用Jfinal2,需要自己写集成代码,自己写一个IMainRenderFactory的实现类

  1. //Jfinal2 集成
  2. import java.io.IOException;
  3. import org.beetl.core.Configuration;
  4. import org.beetl.core.GroupTemplate;
  5. import org.beetl.core.ResourceLoader;
  6. import org.beetl.core.resource.WebAppResourceLoader;
  7. import com.jfinal.kit.PathKit;
  8. import com.jfinal.render.IMainRenderFactory;
  9. import com.jfinal.render.Render;
  10. public class Jfinal2BeetlRenderFactory implements IMainRenderFactory
  11. {
  12. public static String viewExtension = ".html";
  13. public static GroupTemplate groupTemplate = null;
  14. public Jfinal2BeetlRenderFactory()
  15. {
  16. init(PathKit.getWebRootPath());
  17. // init(null); use jfinalkit instead
  18. }
  19. public Jfinal2BeetlRenderFactory(ResourceLoader resourceLoader)
  20. {
  21. if (groupTemplate != null)
  22. {
  23. groupTemplate.close();
  24. }
  25. try
  26. {
  27. Configuration cfg = Configuration.defaultConfiguration();
  28. groupTemplate = new GroupTemplate(resourceLoader, cfg);
  29. }
  30. catch (IOException e)
  31. {
  32. throw new RuntimeException("加载GroupTemplate失败", e);
  33. }
  34. }
  35. public Jfinal2BeetlRenderFactory(String templateRoot)
  36. {
  37. init(templateRoot);
  38. }
  39. private void init(String root)
  40. {
  41. if (groupTemplate != null)
  42. {
  43. groupTemplate.close();
  44. }
  45. try
  46. {
  47. Configuration cfg = Configuration.defaultConfiguration();
  48. WebAppResourceLoader resourceLoader = new WebAppResourceLoader(root);
  49. groupTemplate = new GroupTemplate(resourceLoader, cfg);
  50. }
  51. catch (IOException e)
  52. {
  53. throw new RuntimeException("加载GroupTemplate失败", e);
  54. }
  55. }
  56. public Render getRender(String view)
  57. {
  58. return new BeetlRender(groupTemplate, view);
  59. }
  60. public String getViewExtension()
  61. {
  62. return viewExtension;
  63. }
  64. }

业务逻辑代码:

  1. import org.beetl.core.GroupTemplate;
  2. import org.beetl.core.exception.BeetlException;
  3. import org.beetl.ext.web.WebRender;
  4. import com.jfinal.render.Render;
  5. import com.jfinal.render.RenderException;
  6. //Jfinal2 集成
  7. public class BeetlRender extends Render
  8. {
  9. GroupTemplate gt = null;
  10. private transient static final String encoding = getEncoding();
  11. private transient static final String contentType = "text/html; charset=" + encoding;
  12. public BeetlRender(GroupTemplate gt, String view)
  13. {
  14. this.gt = gt;
  15. this.view = view;
  16. }
  17. @Override
  18. public void render()
  19. {
  20. try
  21. {
  22. response.setContentType(contentType);
  23. WebRender webRender = new WebRender(gt);
  24. webRender.render(view, request, response);
  25. }
  26. catch (BeetlException e)
  27. {
  28. throw new RenderException(e);
  29. }
  30. }
  31. }

然后在Jfinal2里配置完成

  1. import org.beetl.ext.jfinal.BeetlRenderFactory
  2. public class DemoConfig extends JFinalConfig{
  3. public void configConstant(Constants me){
  4. me.setMainRenderFactory(new Jfinal2BeetlRenderFactory());
  5. // 获取GroupTemplate ,可以设置共享变量等操作
  6. GroupTemplate groupTemplate = Jfinal2BeetlRenderFactory.groupTemplate ;
  7. }
  8. }