目前在开发项目时有一个需求,用户可以自己制作模板,详情请看这个反馈http://www.jfinal.com/feedback/3614
假定是user.html(当然需要审核,以防止安全隐患),并且user.html有一个父模板layout.html(项目指定),但是user.html在项目外(不用每次部署备份),layout.html属于项目开发的一部分,在项目内,如下图
感谢波总的耐心解答,下面说一下解决方案
1.创建ExtRender
/** * @author Catch * @date 2018-05-04 12:56 */ public class ExtRender extends TemplateRender { private static final Engine engine = new Engine() .setDevMode(PropKit.getBoolean("devMode", false)) .setBaseTemplatePath(PropKit.get("extBaseTemplatePath")) .addSharedFunction(new FileSource(null, PathKit.getWebRootPath() + "/_view/common/__layout.html")); public ExtRender(String view) { super(view); } @Override public void render() { response.setContentType(getContentType()); Map<Object, Object> data = new HashMap<>(); for (Enumeration<String> attrs = request.getAttributeNames(); attrs.hasMoreElements(); ) { String attrName = attrs.nextElement(); data.put(attrName, request.getAttribute(attrName)); } try { OutputStream os = response.getOutputStream(); engine.getTemplate(view).render(data, os); } catch (RuntimeException e) { // 捕获 ByteWriter.close() 抛出的 RuntimeException Throwable cause = e.getCause(); if (cause instanceof IOException) { // ClientAbortException、EofException 直接或间接继承自 IOException String name = cause.getClass().getSimpleName(); if ("ClientAbortException".equals(name) || "EofException".equals(name)) { return; } } throw e; } catch (IOException e) { throw new RenderException(e); } } }
2.创建BaseController
/** * @author Catch * @date 2018-05-04 12:47 */ public class UserBaseController extends Controller{ public void renderExt(String view) { render(new ExtRender(view)); } }
3.在你的Controller中继承BaseController,然后调用,收工
renderExt("/user.html");