Java Code Examples for com.jfinal.template.stat.Scope#setLocal()

The following examples show how to use com.jfinal.template.stat.Scope#setLocal() . 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: PageDirective.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) {

    String slug = getPara("slug", scope);
    if (StrUtil.isBlank(slug)) {
        throw new IllegalArgumentException("#page(slug = ...) argument must not be empty ");
    }

    SinglePage page = singlePageService.findFirstBySlug(slug);
    if (page == null) {
        return;
    }

    scope.setLocal("page", page);
    renderBody(env, scope, writer);
}
 
Example 2
Source File: RelevantProductsDirective.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) {
    Product product = getPara(0, scope);
    if (product == null) {
        throw new IllegalArgumentException("#relevantProducts(...) argument must not be null or empty." + getLocation());
    }

    //默认值 3
    int count = getParaToInt(1, scope, 3);

    List<Product> relevantProducts = service.findRelevantListByProductId(product.getId(), Product.STATUS_NORMAL, count);

    if (relevantProducts == null || relevantProducts.isEmpty()) {
        return;
    }

    scope.setLocal("relevantProducts", relevantProducts);
    renderBody(env, scope, writer);
}
 
Example 3
Source File: ProductCategoriesDirective.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) {

    Long id = getParaToLong(0, scope);
    String type = getPara(1, scope);

    if (id == null || type == null) {
        throw new IllegalArgumentException("#productCategories() args error. id or type must not be null." + getLocation());
    }


    List<ProductCategory> categories = categoryService.findListByProductId(id, type);
    if (categories == null || categories.isEmpty()) {
        return;
    }

    scope.setLocal("categories", categories);
    renderBody(env, scope, writer);
}
 
Example 4
Source File: RelevantArticlesDirective.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) {
    Article article = getPara(0, scope);
    if (article == null) {
        throw new IllegalArgumentException("#relevantArticles(...) argument must not be null or empty." + getLocation());
    }

    //默认值 3
    int count = getParaToInt(1, scope, 3);

    List<Article> relevantArticles = service.findRelevantListByArticleId(article.getId(), Article.STATUS_NORMAL, count);

    if (relevantArticles == null || relevantArticles.isEmpty()) {
        return;
    }

    scope.setLocal("relevantArticles", relevantArticles);
    renderBody(env, scope, writer);
}
 
Example 5
Source File: ArticleCategoriesDirective.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) {

    Long id = getParaToLong(0, scope);
    String type = getPara(1, scope);

    if (id == null || type == null) {
        throw new IllegalArgumentException("#articleCategories() args error. id or type must not be null." + getLocation());
    }


    List<ArticleCategory> categories = categoryService.findListByArticleId(id, type);
    if (categories == null || categories.isEmpty()) {
        return;
    }

    scope.setLocal("categories", categories);
    renderBody(env, scope, writer);
}
 
Example 6
Source File: ProductsDirective.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) {

    String flag = getPara("flag", scope);
    String style = getPara("style", scope);
    Boolean hasThumbnail = getParaToBool("hasThumbnail", scope);
    String orderBy = getPara("orderBy", scope, "id desc");
    int count = getParaToInt("count", scope, 10);


    Columns columns = Columns.create("flag", flag);
    columns.add("style", style);

    columns.add("status", Product.STATUS_NORMAL);

    if (hasThumbnail != null) {
        if (hasThumbnail) {
            columns.isNotNull("thumbnail");
        } else {
            columns.isNull("thumbnail");
        }
    }

    List<Product> products = service.findListByColumns(columns, orderBy, count);

    if (products == null || products.isEmpty()) {
        return;
    }

    scope.setLocal("products", products);
    renderBody(env, scope, writer);
}
 
Example 7
Source File: NextProductDirective.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) {
    Product product = JbootControllerContext.get().getAttr("product");

    Product nextProduct = service.findNextById(product.getId());
    if (nextProduct == null) {
        return;
    }

    scope.setLocal("next", nextProduct);
    renderBody(env, scope, writer);
}
 
Example 8
Source File: ProductDirective.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) {
    String idOrSlug = getPara(0, scope);
    Product product = getProduct(idOrSlug);

    if (product == null) {
        return;
    }

    scope.setLocal("product", product);
    renderBody(env, scope, writer);
}
 
Example 9
Source File: TagArticlesDirective.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) {

    String tag = getPara("tag", scope);

    if (StrUtil.isBlank(tag)) {
        throw new IllegalArgumentException("#tagArticles() args is error, tag must not be empty." + getLocation());
    }


    Boolean hasThumbnail = getParaToBool("hasThumbnail", scope);
    String orderBy = getPara("orderBy", scope, "order_number desc,id desc");
    int count = getParaToInt("count", scope, 10);

    ArticleCategory category = categoryService.findFirstByTypeAndSlug(ArticleCategory.TYPE_TAG, tag);
    if (category == null) {
        return;
    }

    scope.setLocal("tag", category);

    List<Article> articles = service.findListByCategoryId(category.getId(), hasThumbnail, orderBy, count);

    if (articles == null || articles.isEmpty()) {
        return;
    }

    scope.setLocal("articles", articles);
    renderBody(env, scope, writer);
}
 
Example 10
Source File: TagsDirective.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) {

    String orderBy = getPara("orderBy", scope, "id desc");
    int count = getParaToInt("count", scope, 10);

    List<ArticleCategory> categories = categoryService.findTagList(orderBy, count);
    if (categories == null || categories.isEmpty()) {
        return;
    }


    scope.setLocal("tags", categories);
    renderBody(env, scope, writer);
}
 
Example 11
Source File: PreviousArticleDirective.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) {
    Article article = JbootControllerContext.get().getAttr("article");

    Article previousArticle = service.findPreviousById(article.getId());
    if (previousArticle == null) {
        return;
    }

    scope.setLocal("previous", previousArticle);
    renderBody(env, scope, writer);
}
 
Example 12
Source File: ArticlesDirective.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) {

    String flag = getPara("flag", scope);
    String style = getPara("style", scope);
    Boolean hasThumbnail = getParaToBool("hasThumbnail", scope);
    String orderBy = getPara("orderBy", scope, "id desc");
    int count = getParaToInt("count", scope, 10);


    Columns columns = Columns.create();

    columns.add("flag", flag);
    columns.add("style", style);
    columns.add("status", Article.STATUS_NORMAL);

    if (hasThumbnail != null) {
        if (hasThumbnail) {
            columns.isNotNull("thumbnail");
        } else {
            columns.isNull("thumbnail");
        }
    }

    List<Article> articles = service.findListByColumns(columns, orderBy, count);

    if (articles == null || articles.isEmpty()) {
        return;
    }

    scope.setLocal("articles", articles);
    renderBody(env, scope, writer);
}
 
Example 13
Source File: ArticleDirective.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) {
    String idOrSlug = getPara(0, scope);
    Article article = getArticle(idOrSlug);

    if (article == null) {
        return;
    }

    scope.setLocal("article", article);
    renderBody(env, scope, writer);
}
 
Example 14
Source File: CategoryArticlesDirective.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) {

    Long categoryId = getParaToLong("categoryId", scope);
    String flag = getPara("categoryFlag", scope);

    if (StrUtil.isBlank(flag) && categoryId == null) {
        throw new IllegalArgumentException("#categoryArticles(categoryFlag=xxx,categoryId=xxx) is error, " +
                "categoryFlag or categoryId must not be empty. " + getLocation());
    }

    Boolean hasThumbnail = getParaToBool("hasThumbnail", scope);
    String orderBy = getPara("orderBy", scope, "order_number desc,id desc");
    int count = getParaToInt("count", scope, 10);

    ArticleCategory category = categoryId != null
            ? categoryService.findById(categoryId)
            : categoryService.findFirstByFlag(flag);
    if (category == null) {
        return;
    }

    scope.setLocal("category", category);

    List<Article> articles = service.findListByCategoryId(category.getId(), hasThumbnail, orderBy, count);
    if (articles == null || articles.isEmpty()) {
        return;
    }

    scope.setLocal("articles", articles);
    renderBody(env, scope, writer);
}
 
Example 15
Source File: NextArticleDirective.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) {
    Article article = JbootControllerContext.get().getAttr("article");

    Article nextArticle = service.findNextById(article.getId());
    if (nextArticle == null) {
        return;
    }

    scope.setLocal("next", nextArticle);
    renderBody(env, scope, writer);
}
 
Example 16
Source File: UserArticlesDirective.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) {

    Long userId = getParaToLong("userId", scope);
    User user = JbootControllerContext.get().getAttr("user");

    if (userId == null && user == null) {
        throw new RuntimeException("#userArticles() args is error,userId must not be null." + getLocation());
    }

    if (userId == null) {
        userId = user.getId();
    }

    String orderBy = getPara("orderBy", scope, "id desc");
    String status = getPara("status", scope, Article.STATUS_NORMAL);
    int count = getParaToInt("count", scope, 10);


    Columns columns = Columns.create("user_id", userId);
    columns.add("status", status);

    List<Article> articles = service.findListByColumns(columns, orderBy, count);

    if (articles == null || articles.isEmpty()) {
        return;
    }

    scope.setLocal("articles", articles);
    renderBody(env, scope, writer);
}
 
Example 17
Source File: CategoryProductsDirective.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) {

    Long categoryId = getParaToLong("categoryId", scope);
    String flag = getPara("categoryFlag", scope);

    if (StrUtil.isBlank(flag) && categoryId == null) {
        throw new IllegalArgumentException("#categoryProducts(categoryProducts=xxx,categoryId=xxx) is error, " +
                "categoryFlag or categoryId must not be empty. " + getLocation());
    }

    Boolean hasThumbnail = getParaToBool("hasThumbnail", scope);
    String orderBy = getPara("orderBy", scope, "order_number desc,id desc");
    int count = getParaToInt("count", scope, 10);

    ProductCategory category = categoryId != null
            ? categoryService.findById(categoryId)
            : categoryService.findFirstByFlag(flag);
    if (category == null) {
        return;
    }

    scope.setLocal("category", category);

    List<Product> products = service.findListByCategoryId(category.getId(), hasThumbnail, orderBy, count);
    if (products == null || products.isEmpty()) {
        return;
    }

    scope.setLocal("products", products);
    renderBody(env, scope, writer);
}
 
Example 18
Source File: PreviousProductDirective.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) {
    Product product = JbootControllerContext.get().getAttr("product");

    Product previousProduct = service.findPreviousById(product.getId());
    if (previousProduct == null) {
        return;
    }

    scope.setLocal("previous", previousProduct);
    renderBody(env, scope, writer);
}
 
Example 19
Source File: ProductTagListDirective.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) {

    String orderBy = getPara("orderBy", scope, "id desc");
    int count = getParaToInt("count", scope, 10);

    List<ProductCategory> categories = categoryService.findListByType(ProductCategory.TYPE_TAG, orderBy, count);
    if (categories == null || categories.isEmpty()) {
        return;
    }

    scope.setLocal("tags", categories);
    renderBody(env, scope, writer);
}
 
Example 20
Source File: PaginateDirectiveBase.java    From jboot with Apache License 2.0 4 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    String previousClass = getPara(PREVIOUS_CLASS_KEY, scope, DEFAULT_PREVIOUS_CLASS);
    String nextClass = getPara(NEXT_CLASS_KEY, scope, DEFAULT_NEXT_CLASS);
    String activeClass = getPara(ACTIVE_CLASS_KEY, scope, DEFAULT_ACTIVE_CLASS);
    String disabledClass = getPara(DISABLED_CLASS_KEY, scope, DEFAULT_DISABLED_CLASS);
    boolean onlyShowPreviousAndNext = getParaToBool(ONLY_SHOW_PREVIOUS_AND_NEXT_KEY, scope, false);

    String previousText = getPara(PREVIOUS_TEXT_KEY, scope, DEFAULT_PREVIOUS_TEXT);
    String nextText = getPara(NEXT_TEXT_KEY, scope, DEFAULT_NEXT_TEXT);
    String pageItemsName = getPara(PAGE_ITEMS_NAME_KEY, scope, DEFAULT_PAGE_ITEMS_NAME);


    Page<?> page = getPage(env, scope, writer);

    int currentPage = page == null ? 1 : page.getPageNumber();
    int totalPage = page == null ? 1 : page.getTotalPage();

    if ((totalPage <= 0) || (currentPage > totalPage)) {
        return;
    }

    int startPage = currentPage - 4;
    if (startPage < 1) {
        startPage = 1;
    }
    int endPage = currentPage + 4;
    if (endPage > totalPage) {
        endPage = totalPage;
    }

    if (currentPage <= 8) {
        startPage = 1;
    }

    if ((totalPage - currentPage) < 8) {
        endPage = totalPage;
    }

    List<PaginateItem> pages = new ArrayList<PaginateItem>();
    if (currentPage == 1) {
        pages.add(new PaginateDirectiveBase.PaginateItem(previousClass + StrUtil.SPACE + disabledClass, JAVASCRIPT_TEXT, previousText));
    } else {
        pages.add(new PaginateDirectiveBase.PaginateItem(previousClass, getUrl(currentPage - 1, env, scope, writer), previousText));
    }

    if (currentPage > 8 && !onlyShowPreviousAndNext) {
        pages.add(new PaginateDirectiveBase.PaginateItem(StrUtil.EMPTY, getUrl(1, env, scope, writer), 1));
        pages.add(new PaginateDirectiveBase.PaginateItem(StrUtil.EMPTY, getUrl(2, env, scope, writer), 2));
        pages.add(new PaginateDirectiveBase.PaginateItem(disabledClass, JAVASCRIPT_TEXT, ELLIPSIS_TEXT));
    }

    if (!onlyShowPreviousAndNext) {
        for (int i = startPage; i <= endPage; i++) {
            if (currentPage == i) {
                pages.add(new PaginateDirectiveBase.PaginateItem(activeClass, JAVASCRIPT_TEXT, i));
            } else {
                pages.add(new PaginateDirectiveBase.PaginateItem(StrUtil.EMPTY, getUrl(i, env, scope, writer), i));
            }
        }
    }

    if ((totalPage - currentPage) >= 8 && !onlyShowPreviousAndNext) {
        pages.add(new PaginateDirectiveBase.PaginateItem(disabledClass, JAVASCRIPT_TEXT, ELLIPSIS_TEXT));
        pages.add(new PaginateDirectiveBase.PaginateItem(StrUtil.EMPTY, getUrl(totalPage - 1, env, scope, writer), totalPage - 1));
        pages.add(new PaginateDirectiveBase.PaginateItem(StrUtil.EMPTY, getUrl(totalPage, env, scope, writer), totalPage));
    }

    if (currentPage == totalPage) {
        pages.add(new PaginateDirectiveBase.PaginateItem(nextClass + StrUtil.SPACE + disabledClass, JAVASCRIPT_TEXT, nextText));
    } else {
        pages.add(new PaginateDirectiveBase.PaginateItem(nextClass, getUrl(currentPage + 1, env, scope, writer), nextText));
    }

    scope.setLocal(pageItemsName, pages);
    renderBody(env, scope, writer);
}