Java Code Examples for com.jfinal.core.Controller#render()
The following examples show how to use
com.jfinal.core.Controller#render() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LimitFallbackProcesserDefault.java From jboot with Apache License 2.0 | 6 votes |
/** * 处理 Controller 的限流 * * @param resource * @param inv */ protected void doProcessWebLimit(String resource, Invocation inv) { Controller controller = inv.getController(); controller.getResponse().setStatus(config.getDefaultHttpCode()); if (RequestUtil.isAjaxRequest(controller.getRequest())) { controller.renderJson(config.getDefaultAjaxContent()); } //非ajax的正常请求 else { String limitView = config.getDefaultHtmlView(); if (limitView != null) { controller.render(limitView); } else { controller.renderText("reqeust limit."); } } }
Example 2
Source File: BlogValidator.java From sqlhelper with GNU Lesser General Public License v3.0 | 5 votes |
protected void handleError(Controller controller) { controller.keepModel(Blog.class); String actionKey = getActionKey(); if (actionKey.equals("/blog/save")) controller.render("add.html"); else if (actionKey.equals("/blog/update")) controller.render("edit.html"); }
Example 3
Source File: ParaValidateInterceptor.java From jboot with Apache License 2.0 | 5 votes |
private void renderError(Controller controller, String renderType, String formName, String message, String redirectUrl, String htmlPath, int errorCode) { switch (renderType) { case ValidateRenderType.DEFAULT: if (RequestUtil.isAjaxRequest(controller.getRequest())) { controller.renderJson( Ret.fail("message", message) .set("errorCode", errorCode) .setIfNotNull("formName", formName) ); } else { controller.renderError(404); } break; case ValidateRenderType.JSON: controller.renderJson( Ret.fail("message", message) .set("errorCode", errorCode) .setIfNotNull("formName", formName) ); break; case ValidateRenderType.REDIRECT: controller.redirect(redirectUrl); break; case ValidateRenderType.HTML: controller.render(htmlPath); break; case ValidateRenderType.TEXT: controller.renderText(message); break; default: throw new IllegalArgumentException("can not process render : " + renderType); } }
Example 4
Source File: PermissionInterceptor.java From my_curd with Apache License 2.0 | 5 votes |
@Override public void intercept(Invocation inv) { boolean flag = true; Controller controller = inv.getController(); // 验证菜单权限 RequirePermission requirePermission = inv.getClass().getAnnotation(RequirePermission.class); List<String> codes; if (requirePermission != null ) { codes = requirePermission.isPermission() ? controller.getSessionAttr("menuCodes") : controller.getSessionAttr("roleCodes"); flag = codes.contains(requirePermission.value()); } if (flag) { // 菜单权限通后 再验证按钮权限 requirePermission = inv.getMethod().getAnnotation(RequirePermission.class); if (requirePermission != null) { codes = requirePermission.isPermission() ? controller.getSessionAttr("buttonCodes") : controller.getSessionAttr("roleCodes"); flag = codes.contains(requirePermission.value()); } } if (flag) { // 菜单权限、按钮权限 都具备 放行 inv.invoke(); return; } // 无权限响应 String requestType = inv.getController().getHeader("X-Requested-With"); if ("XMLHttpRequest".equals(requestType) || StringUtils.notEmpty(inv.getController().getPara("xmlHttpRequest"))) { Ret ret = Ret.create().setFail().set("msg", "无权限操作!您的行为已被记录到日志。"); // 其实并没有,可以自行扩展 controller.renderJson(ret); } else { controller.render("/WEB-INF/views/common/no_permission.ftl"); } }
Example 5
Source File: AdminInterceptor.java From zrlog with Apache License 2.0 | 5 votes |
/** * 为了规范代码,这里做了一点类是Spring的ResponseEntity的东西,及通过方法的返回值来判断是应该返回页面还会对应JSON数据 * 具体方式看 AdminRouters,这里用到了 ThreadLocal * * @param ai */ private void adminPermission(Invocation ai) { Controller controller = ai.getController(); AdminTokenVO adminTokenVO = adminTokenService.getAdminTokenVO(controller.getRequest()); if (adminTokenVO != null) { try { User user = new User().findById(adminTokenVO.getUserId()); if (StringUtils.isEmpty(user.getStr("header"))) { user.set("header", Constants.DEFAULT_HEADER); } controller.setAttr("user", user); controller.setAttr("protocol", adminTokenVO.getProtocol()); TemplateHelper.fullTemplateInfo(controller, false); if (!"/admin/logout".equals(ai.getActionKey())) { adminTokenService.setAdminToken(user, adminTokenVO.getSessionId(), adminTokenVO.getProtocol(), controller.getRequest(), controller.getResponse()); } ai.invoke(); // 存在消息提示 if (controller.getAttr("message") != null) { initIndex(controller.getRequest()); controller.render(new FreeMarkerRender("/admin/index.ftl")); } else { if (!tryDoRender(ai, controller)) { controller.renderHtml(IOUtil.getStringInputStream(new FileInputStream(PathKit.getWebRootPath() + Constants.NOT_FOUND_PAGE))); } } } catch (Exception e) { LOGGER.error("", e); exceptionHandler(ai, e); } finally { AdminTokenThreadLocal.remove(); } } else if ("/admin/login".equals(ai.getActionKey()) || "/api/admin/login".equals(ai.getActionKey())) { ai.invoke(); tryDoRender(ai, controller); } else { blockUnLoginRequestHandler(ai); } }
Example 6
Source File: AdminInterceptor.java From zrlog with Apache License 2.0 | 5 votes |
/** * 在重定向到登陆页面时,同时携带了当前的请求路径,方便登陆成功后的跳转 */ private void loginRender(Controller controller) throws MalformedURLException { HttpServletRequest request = controller.getRequest(); String url = controller.getRequest().getRequestURL().toString(); URL tUrl = new URL(url); previewField(controller.getRequest()); controller.getRequest().setAttribute("redirectFrom", tUrl.getPath() + (request.getQueryString() != null ? "?" + request.getQueryString() : "")); controller.render(new FreeMarkerRender("/admin/login.ftl")); }
Example 7
Source File: AdminInterceptor.java From zrlog with Apache License 2.0 | 5 votes |
/** * 尝试通过Controller的放回值来进行数据的渲染 * * @param ai * @param controller * @return true 表示已经渲染数据了,false 表示并未按照约定编写,及没有进行渲染 */ private boolean tryDoRender(Invocation ai, Controller controller) { Object returnValue = ai.getReturnValue(); if (ai.getMethod().getAnnotation(RefreshCache.class) != null) { cacheService.refreshInitDataCache(GlobalResourceHandler.CACHE_HTML_PATH, controller, true); if (JFinal.me().getConstants().getDevMode()) { LOGGER.info("{} trigger refresh cache", controller.getRequest().getRequestURI()); } } boolean rendered = false; if (returnValue != null) { if (ai.getActionKey().startsWith("/api/admin")) { controller.renderJson((Object) ai.getReturnValue()); rendered = true; } else if (ai.getActionKey().startsWith("/admin") && returnValue instanceof String) { //返回值,约定:admin 开头的不写模板类型,其他要写全 if (!returnValue.toString().endsWith(".jsp") && returnValue.toString().startsWith("/admin")) { String templatePath = returnValue.toString() + ".ftl"; if (AdminInterceptor.class.getResourceAsStream(Constants.FTL_VIEW_PATH + templatePath) != null) { controller.render(new FreeMarkerRender(templatePath)); rendered = true; } else { rendered = false; } } else { controller.render(returnValue.toString()); rendered = true; } } } else { rendered = true; } return rendered; }