Java Code Examples for com.jfinal.core.Controller#getAttr()

The following examples show how to use com.jfinal.core.Controller#getAttr() . 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: SmartField.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Object doGetDataFromControllerByName(String name) {
    Controller controller = JbootControllerContext.get();
    if (name.contains(".")) {
        String[] modelAndAttr = name.split("\\.");
        String modelName = modelAndAttr[0];
        String attr = modelAndAttr[1];
        Object object = controller.getAttr(modelName);
        if (object == null) {
            return null;
        } else if (object instanceof Model) {
            return ((Model) object).get(attr);
        } else if (object instanceof Map) {
            return ((Map) object).get(attr);
        } else {
            try {
                Method method = object.getClass().getMethod("get" + StrKit.firstCharToUpperCase(attr));
                return method.invoke(object);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    } else {
        return controller.getAttr(name);
    }
}
 
Example 2
Source File: ArticlePageDirective.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    Controller controller = JbootControllerContext.get();

    int page = controller.getParaToInt(1, 1);
    int pageSize = getParaToInt("pageSize", scope, 10);
    String orderBy = getPara("orderBy", scope, "id desc");

    // 可以指定当前的分类ID
    Long categoryId = getParaToLong("categoryId", scope, 0L);
    ArticleCategory category = controller.getAttr("category");

    if (categoryId == 0 && category != null) {
        categoryId = category.getId();
    }

    Page<Article> articlePage = categoryId == 0
            ? service.paginateInNormal(page, pageSize, orderBy)
            : service.paginateByCategoryIdInNormal(page, pageSize, categoryId, orderBy);

    scope.setGlobal("articlePage", articlePage);
    renderBody(env, scope, writer);
}
 
Example 3
Source File: ArticleSearchPageDirective.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    Controller controller = JbootControllerContext.get();

    String keyword = controller.getAttr("keyword");
    int page = controller.getAttr("page");
    int pageSize = getParaToInt("pageSize", scope, 10);

    Page<Article> dataPage = StrUtil.isNotBlank(keyword)
            ? articleService.search(keyword, page, pageSize)
            : null;

    if (dataPage != null) {
        scope.setGlobal("articlePage", dataPage);
    }

    //需要页面自行判断 articlePage 是否为空
    renderBody(env, scope, writer);
}
 
Example 4
Source File: ProductPageDirective.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    Controller controller = JbootControllerContext.get();

    int page = controller.getParaToInt(1, 1);
    int pageSize = getParaToInt("pageSize", scope, 10);
    String orderBy = getPara("orderBy", scope, "id desc");

    // 可以指定当前的分类ID
    Long categoryId = getParaToLong("categoryId", scope, 0L);
    ProductCategory category = controller.getAttr("category");

    if (categoryId == 0 && category != null) {
        categoryId = category.getId();
    }

    Page<Product> productPage = categoryId == 0
            ? service.paginateInNormal(page, pageSize, orderBy)
            : service.paginateByCategoryIdInNormal(page, pageSize, categoryId, orderBy);

    scope.setGlobal("productPage", productPage);
    renderBody(env, scope, writer);
}
 
Example 5
Source File: ProductSearchPageDirective.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    Controller controller = JbootControllerContext.get();

    String keyword = controller.getAttr("keyword");
    int page = controller.getAttr("page");
    int pageSize = getParaToInt("pageSize", scope, 10);

    Page<Product> dataPage = StrUtil.isNotBlank(keyword)
            ? productService.search(keyword, page, pageSize)
            : null;

    if (dataPage != null) {
        scope.setGlobal("productPage", dataPage);
    }

    //需要页面自行判断 productPage 是否为空
    renderBody(env, scope, writer);
}
 
Example 6
Source File: UserInterceptor.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void intercept(Invocation inv) {

    Controller c = inv.getController();
    User user = c.getAttr(JPressConsts.ATTR_LOGINED_USER);

    if (user != null) {

        //购物车的相关信息
        setUserCartInfoAttrs(inv, user);

        inv.invoke();
        return;
    }


    String uid = CookieUtil.get(c, JPressConsts.COOKIE_UID);
    if (StrUtil.isBlank(uid)) {
        inv.invoke();
        return;
    }

    user = userService.findById(uid);

    if (user != null) {
        c.setAttr(JPressConsts.ATTR_LOGINED_USER, user);
        setUserCartInfoAttrs(inv, user);

    }

    inv.invoke();
}
 
Example 7
Source File: PageCommentPageDirective.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    Controller controller = JbootControllerContext.get();

    int page = controller.getParaToInt(1, 1);
    int pageSize = getParaToInt("pageSize", scope, 10);

    SinglePage singlePage = controller.getAttr("page");
    if (singlePage != null) {
        Page<SinglePageComment> articlePage = service.paginateByPageIdInNormal(page, pageSize, singlePage.getId());
        scope.setGlobal("commentPage", articlePage);
        renderBody(env, scope, writer);
    }
}
 
Example 8
Source File: CommentPageDirective.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    Controller controller = JbootControllerContext.get();

    int page = controller.getParaToInt(1, 1);
    int pageSize = getParaToInt("pageSize", scope, 10);

    Article article = controller.getAttr("article");
    if (article != null) {
        Page<ArticleComment> articlePage = service.paginateByArticleIdInNormal(page, pageSize, article.getId());
        scope.setGlobal("commentPage", articlePage);
        renderBody(env, scope, writer);
    }
}
 
Example 9
Source File: ProductCommentPageDirective.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    Controller controller = JbootControllerContext.get();

    int page = controller.getParaToInt(1, 1);
    int pageSize = getParaToInt("pageSize", scope, 10);

    Product product = controller.getAttr("product");
    if (product != null) {
        Page<ProductComment> articlePage = service.paginateByProductIdInNormal(page, pageSize, product.getId());
        scope.setGlobal("commentPage", articlePage);
        renderBody(env, scope, writer);
    }
}
 
Example 10
Source File: AdminInterceptor.java    From zrlog with Apache License 2.0 5 votes vote down vote up
/**
 * 为了规范代码,这里做了一点类是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);
    }
}