在设计cms系统的时候,如果有自定义模板的时候,后台配置了模板地址的话,但是文件中模板不存在的话,需要给予提示,不能显示空白页面。
1.自定义 renderFactory接管
/** * @Description: * @Author: Robin Xia * @Date: Created on 2017/12/11 */ public class CustomRenderFactory extends RenderFactory { @Override public Render getRender(String view) { return new CustomRender(view); } }
2.实现render方法,扑捉异常处理异常
/** * @Description: * @Author: Robin Xia * @Date: Created on 2017/12/11 */ public class CustomRender extends TemplateRender{ public CustomRender(String view) { super(view); } @Override public void render() { response.setContentType(getContentType()); Map<Object, Object> data = new HashMap<Object, Object>(); for (Enumeration<String> attrs = request.getAttributeNames(); attrs.hasMoreElements();) { String attrName = attrs.nextElement(); data.put(attrName, request.getAttribute(attrName)); } try { OutputStream os = response.getOutputStream(); Template template = null; try { template = RenderManager.me().getEngine().getTemplate(view); template.render(data, os); }catch (Exception e){ Map<Object, Object> errorMap = new HashMap<Object, Object>(); errorMap.put("message","未找到模板文件,路径为:" + view); String errorView = SystemConstant.TEMPLATES_COMMON_PATH + File.separator + SystemConstant.ERROR_PAGE; RenderManager.me().getEngine().getTemplate(errorView) .render( errorMap, os); } }catch (Exception e) { throw new RenderException(e); } } }
3.config配置一下
@Override public void configConstant(Constants me) { me.setDevMode(p.getBoolean("devMode", false)); me.setJsonFactory(MixedJsonFactory.me()); me.setRenderFactory(new CustomRenderFactory()); }
4.配置一个错误页面,显示错误提示即可