Java Code Examples for com.jfinal.core.Controller#renderJson()
The following examples show how to use
com.jfinal.core.Controller#renderJson() .
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: TokenInterceptor.java From jfinal-api-scaffold with MIT License | 6 votes |
@Override public void intercept(ActionInvocation ai) { Controller controller = ai.getController(); String token = controller.getPara("token"); if (StringUtils.isEmpty(token)) { controller.renderJson(new BaseResponse(Code.ARGUMENT_ERROR, "token can not be null")); return; } User user = TokenManager.getMe().validate(token); if (user == null) { controller.renderJson(new BaseResponse(Code.TOKEN_INVALID, "token is invalid")); return; } controller.setAttr("user", user); ai.invoke(); }
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: ProductValidate.java From jpress with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void intercept(Invocation inv) { Controller c = inv.getController(); Long productId = inv.getController().getLong("id"); Product product = productService.findById(productId); if (product == null || !product.isNormal()) { if (RequestUtil.isAjaxRequest(c.getRequest())) { c.renderJson(Ret.fail().set("code", "2").set("message", "商品不存在或已下架。")); } else { c.renderError(404); } return; } User user = UserInterceptor.getThreadLocalUser(); if (user == null) { if (RequestUtil.isAjaxRequest(c.getRequest())) { c.renderJson(Ret.fail() .set("code", 1) .set("message", "用户未登录") .set("gotoUrl", JFinal.me().getContextPath() + "/user/login?gotoUrl=" + product.getUrl())); } else { c.redirect("/user/login?gotoUrl=" + product.getUrl()); } return; } c.setAttr(ATTR_PRODUCT,product); inv.invoke(); }
Example 6
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; }
Example 7
Source File: JsonValidator.java From jboot-admin with Apache License 2.0 | 4 votes |
@Override protected void handleError(Controller c) { RestResult<String> result = new RestResult<String>(); result.error(c.getAttr(ERROR_MSG_KEY).toString()); c.renderJson(result); }
Example 8
Source File: IdsRequired.java From my_curd with Apache License 2.0 | 4 votes |
@Override protected void handleError(Controller c) { Ret ret = Ret.create().setFail().set("msg", "ids 参数为空"); c.renderJson(ret); }
Example 9
Source File: IdRequired.java From my_curd with Apache License 2.0 | 4 votes |
@Override protected void handleError(Controller c) { Ret ret = Ret.create().setFail().set("msg", "id 参数为空"); c.renderJson(ret); }
Example 10
Source File: JwtShiroInvokeListener.java From jboot-admin with Apache License 2.0 | 2 votes |
/** * 其他处理 * @param controller */ private void doProcessuDefault(Controller controller) { controller.renderJson(RestResult.buildError("404")); }
Example 11
Source File: JwtShiroInvokeListener.java From jboot-admin with Apache License 2.0 | 2 votes |
/** * 没有认证信息处理 * @param controller */ private void doProcessUnauthenticated(Controller controller) { controller.renderJson(RestResult.buildError("401")); }
Example 12
Source File: JwtShiroInvokeListener.java From jboot-admin with Apache License 2.0 | 2 votes |
/** * 无授权信息处理 * @param controller */ private void doProcessuUnauthorization(Controller controller) { controller.renderJson(RestResult.buildError("403")); }