4.8. JFinal3.0&JFinal2.o集成方案
Beetl提供 JFinal3.0 集成,使用JFinal3BeetlRenderFactory ,通过如下注册即可使用beetl模板引擎
public class DemoConfig extends JFinalConfig {
public void configConstant(Constants me) {
PropKit.use("a_little_config.txt"); // 加载少量必要配置,随后可用PropKit.get(...)获取值
me.setDevMode(PropKit.getBoolean("devMode", false));
JFinal3BeetlRenderFactory rf = new JFinal3BeetlRenderFactory();
rf.config();
me.setRenderFactory(rf);
GroupTemplate gt = rf.groupTemplate;
//根据gt可以添加扩展函数,格式化函数,共享变量等,
}
业务逻辑代码:
public void modify(){
int artId = getParaToInt(0, -1);
setAttr("title", "修改文章");
List<Cate> cateLists = Cate.getAllCate();
//模板里访问cateLists,atr,
setAttr("cateLists", cateLists);
setAttr("art", Article.dao.findById(artId));
render("/modify.html");
}
BeetlRenderFactory 默认使用FileResourceLoader ,其根目录位于WebRoot目录下,如果你需要修改到别的目录,可以设置配置文件,如
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的实现类
//Jfinal2 集成
import java.io.IOException;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.ResourceLoader;
import org.beetl.core.resource.WebAppResourceLoader;
import com.jfinal.kit.PathKit;
import com.jfinal.render.IMainRenderFactory;
import com.jfinal.render.Render;
public class Jfinal2BeetlRenderFactory implements IMainRenderFactory
{
public static String viewExtension = ".html";
public static GroupTemplate groupTemplate = null;
public Jfinal2BeetlRenderFactory()
{
init(PathKit.getWebRootPath());
// init(null); use jfinalkit instead
}
public Jfinal2BeetlRenderFactory(ResourceLoader resourceLoader)
{
if (groupTemplate != null)
{
groupTemplate.close();
}
try
{
Configuration cfg = Configuration.defaultConfiguration();
groupTemplate = new GroupTemplate(resourceLoader, cfg);
}
catch (IOException e)
{
throw new RuntimeException("加载GroupTemplate失败", e);
}
}
public Jfinal2BeetlRenderFactory(String templateRoot)
{
init(templateRoot);
}
private void init(String root)
{
if (groupTemplate != null)
{
groupTemplate.close();
}
try
{
Configuration cfg = Configuration.defaultConfiguration();
WebAppResourceLoader resourceLoader = new WebAppResourceLoader(root);
groupTemplate = new GroupTemplate(resourceLoader, cfg);
}
catch (IOException e)
{
throw new RuntimeException("加载GroupTemplate失败", e);
}
}
public Render getRender(String view)
{
return new BeetlRender(groupTemplate, view);
}
public String getViewExtension()
{
return viewExtension;
}
}
业务逻辑代码:
import org.beetl.core.GroupTemplate;
import org.beetl.core.exception.BeetlException;
import org.beetl.ext.web.WebRender;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
//Jfinal2 集成
public class BeetlRender extends Render
{
GroupTemplate gt = null;
private transient static final String encoding = getEncoding();
private transient static final String contentType = "text/html; charset=" + encoding;
public BeetlRender(GroupTemplate gt, String view)
{
this.gt = gt;
this.view = view;
}
@Override
public void render()
{
try
{
response.setContentType(contentType);
WebRender webRender = new WebRender(gt);
webRender.render(view, request, response);
}
catch (BeetlException e)
{
throw new RenderException(e);
}
}
}
然后在Jfinal2里配置完成
import org.beetl.ext.jfinal.BeetlRenderFactory
public class DemoConfig extends JFinalConfig{
public void configConstant(Constants me){
me.setMainRenderFactory(new Jfinal2BeetlRenderFactory());
// 获取GroupTemplate ,可以设置共享变量等操作
GroupTemplate groupTemplate = Jfinal2BeetlRenderFactory.groupTemplate ;
}
}
当前内容版权归 ibeetl.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 ibeetl.com .