com.jfinal.template.stat.Scope Java Examples

The following examples show how to use com.jfinal.template.stat.Scope. 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: ProductPageDirective.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) {
    HttpServletRequest request = JbootControllerContext.get().getRequest();
    String url = request.getRequestURI();
    String contextPath = JFinal.me().getContextPath();

    boolean firstGotoIndex = getPara("firstGotoIndex", scope, false);

    if (pageNumber == 1 && firstGotoIndex) {
        return contextPath + "/";
    }

    // 如果当前页面是首页的话
    // 需要改变url的值,因为 上一页或下一页是通过当前的url解析出来的
    if (url.equals(contextPath + "/")) {
        url = contextPath + "/product/category/index" + JPressOptions.getAppUrlSuffix();
    }

    return DirectveKit.replacePageNumber(url, pageNumber);
}
 
Example #2
Source File: RoleDirective.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) {

    User user = UserInterceptor.getThreadLocalUser();
    if (user == null || !user.isStatusOk()) {
        return;
    }


    Set<String> roles = StrUtil.splitToSet(getPara(0, scope), ",");
    if (roles == null || roles.size() == 0) {
        throw new IllegalArgumentException("#role(...) argument must not be empty");
    }

    if (roleService.hasRole(user.getId(), roles.toArray(new String[]{}))) {
        renderBody(env, scope, writer);
    }

}
 
Example #3
Source File: AnyRoleDirective.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) {

    User user = UserInterceptor.getThreadLocalUser();
    if (user == null || !user.isStatusOk()) {
        return;
    }


    Set<String> roles = StrUtil.splitToSet(getPara(0, scope), ",");
    if (roles == null || roles.size() == 0) {
        throw new IllegalArgumentException("#anyRole(...) argument must not be empty");
    }

    if (roleService.hasAnyRole(user.getId(), roles.toArray(new String[]{}))) {
        renderBody(env, scope, writer);
    }
}
 
Example #4
Source File: MaxLengthDirective.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 content = getPara(0, scope);
    if (StrUtil.isBlank(content)) {
        return;
    }

    int maxLength = getParaToInt(1, scope, 0);
    if (maxLength <= 0) {
        throw new IllegalArgumentException("#maxLength(content,length) 参数错误,length必须大于0 " + getLocation());
    }

    String suffix = getPara(2, scope);
    try {
        writer.write(CommonsUtils.maxLength(content, maxLength, suffix));
    } catch (IOException e) {
        throw new TemplateException(e.getMessage(), location, e);
    }
}
 
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: ParaDirective.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 key = getPara(0, scope);
    String defaultValue = getPara(1, scope);

    if (StrUtil.isBlank(key)) {
        throw new IllegalArgumentException("#para(...) argument must not be empty" + getLocation());
    }

    String value = controller.getPara(key);
    if (StrUtil.isBlank(value)) {
        value = StrUtil.isNotBlank(defaultValue) ? defaultValue : "";
    }

    try {
        writer.write(value);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #7
Source File: EditorContentDirective.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 originalContent = getPara(0, scope);
    if (originalContent == null) {
        return;
    }

    String editMode = JbootControllerContext.get().getAttr("editMode");
    if (StrUtil.isNotBlank(editMode) && !JPressConsts.EDIT_MODE_HTML.equals(editMode)){
        renderText(writer,originalContent);
    }
    //ckeditor 编辑器有个bug,自动把 &lt; 转化为 < 和 把 &gt; 转化为 >
    //因此,此处需要 把 "&lt;" 替换为 "&amp;lt;" 和 把 "&gt;" 替换为 "&amp;gt;"
    //方案:http://komlenic.com/246/encoding-entities-to-work-with-ckeditor-3/
    else {
        renderText(writer, StringUtils.replaceEach(originalContent, originalChars, newChars));
    }
}
 
Example #8
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 #9
Source File: ProductTagsDirective.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);

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


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

    scope.setLocal("tags", categories);
    renderBody(env, scope, writer);
}
 
Example #10
Source File: PagesDirective.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 flag = getPara("flag", scope);

    List<SinglePage> singlePages = StrUtil.isBlank(flag)
            ? singlePageService.findAll()
            : singlePageService.findListByFlag(flag);

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

    //设置页面高亮
    doFlagIsActiveByCurrentPage(singlePages);

    scope.setLocal("pages", singlePages);

    renderBody(env, scope, writer);
}
 
Example #11
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 #12
Source File: ShiroHasAllPermissionDirective.java    From jboot with Apache License 2.0 6 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {
    if (getSubject() != null && ArrayUtil.isNotEmpty(exprList.getExprArray())) {
        boolean hasAllPermission = true;
        for (Expr expr : exprList.getExprArray()) {
            if (!getSubject().isPermitted(expr.eval(scope).toString())) {
                hasAllPermission = false;
                break;
            }
        }

        if (hasAllPermission) {
            renderBody(env, scope, writer);
        }

    }
}
 
Example #13
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 #14
Source File: CommentsDirective.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 orderBy = getPara("orderBy", scope, "id desc");
    int count = getParaToInt("count", scope, 10);

    Columns columns = Columns.create("status", ArticleComment.STATUS_NORMAL);
    List<ArticleComment> comments = service.findListByColumns(columns, orderBy, count);

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

    scope.setLocal("comments", comments);
    renderBody(env, scope, writer);
}
 
Example #15
Source File: StatusTplDirective.java    From jboot-admin with Apache License 2.0 6 votes vote down vote up
@Override
public void exec(Env env, Scope scope, Writer writer) {
    if (exprList.length() > 2) {
        throw new ParseException("Wrong number parameter of #date directive, two parameters allowed at most", location);
    }

    status = getParam(0, scope);
    if (status == null) {
        throw new ParseException("status is null", location);
    }

    if (exprList.length() > 1) {
        attrName = getParam(1, "status", scope);
    }

    write(writer, "<div>");
    for (String key : status.all().keySet()) {
        write(writer, "{{#  if(d." + attrName + " == \\'" + key + "\\') { }}");
        write(writer, status.desc(key));
        write(writer, "{{#  } }}");
    }
    write(writer, "</div>");
}
 
Example #16
Source File: OptionStatusDirective.java    From jboot-admin with Apache License 2.0 6 votes vote down vote up
@Override
public void exec(Env env, Scope scope, Writer writer) {
    if (exprList.length() > 2) {
        throw new ParseException("Wrong number parameter of #date directive, two parameters allowed at most", location);
    }

    status = getParam(0, scope);
    if (status == null) {
        throw new ParseException("status is null", location);
    }

    if (exprList.length() > 1) {
        value = getParam(1, "", scope);
    }

    for (String key : status.all().keySet()) {
        if (value != null && key.equals(value)) {
            write(writer, "<option selected value=\"" + key  + "\">" + status.desc(key) + "</option>");
        } else {
            write(writer, "<option value=\"" + key  + "\">" + status.desc(key) + "</option>");
        }
    }
}
 
Example #17
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 #18
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 #19
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 #20
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 #21
Source File: HasAddonDirective.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 id = getPara(0, scope);

    if (StrUtil.isBlank(id)) {
        throw new IllegalArgumentException("#hasAddon(...) argument must not be empty" + getLocation());
    }

    AddonInfo addonInfo = AddonManager.me().getAddonInfo(id);
    if (addonInfo != null && addonInfo.isStarted()){
        renderBody(env,scope,writer);
    }
}
 
Example #22
Source File: DataDirective.java    From jboot-admin with Apache License 2.0 5 votes vote down vote up
@Override
public void exec(Env env, Scope scope, Writer writer) {
    if (exprList.length() > 2) {
        throw new ParseException("Wrong number parameter of #date directive, two parameters allowed at most", location);
    }

    typeCode = getParam(0, scope);
    if (StrKit.isBlank(typeCode)) {
        throw new ParseException("typeCode is null", location);
    }

    if (exprList.length() > 1) {
        code = getParam(1,scope);
    }



    List<Data> list = dataApi.getListByTypeOnUse(typeCode);

    if (StrKit.isBlank(code)) {
        write(writer, "");
    } else {
        for (Data data : list) {
            if(code.equals(data.getCode())) {
                write(writer,data.getCodeDesc());
            }
        }
    }
}
 
Example #23
Source File: SelectedIfDirective.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) {
    Object param = getPara(0, scope);
    if (param instanceof Boolean) {
        if ((Boolean) param) {
            renderText(writer, "selected");
        }
    } else if ("true".equalsIgnoreCase(String.valueOf(param))) {
        renderText(writer, "selected");
    }
}
 
Example #24
Source File: CheckedIfDirective.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) {
    Object param = getPara(0, scope);
    if (param instanceof Boolean) {
        if ((Boolean) param) {
            renderText(writer,"checked");
        }
    } else if ("true".equalsIgnoreCase(String.valueOf(param))) {
        renderText(writer,"checked");
    }
}
 
Example #25
Source File: ShiroHasPermissionDirective.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {
    if (getSubject() != null && ArrayUtil.isNotEmpty(exprList.getExprArray())) {
        if (getSubject().isPermitted(getPara(0, scope).toString())) {
            renderBody(env, scope, writer);
        }
    }

}
 
Example #26
Source File: StrSplit.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 orginalStr = getPara(0, scope);
    if (StrUtil.isBlank(orginalStr)) {
        return;
    }

    String splitStr = getPara(1, scope, ",");
    Set<String> strs = StrUtil.splitToSet(orginalStr,splitStr);

    scope.setLocal("strs", strs);
    renderBody(env, scope, writer);
}
 
Example #27
Source File: MessageListDirective.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) {

    Columns columns = new Columns();
    columns.eq("show", true);
    List<JpressAddonMessage> jpressAddonMessageList = service.findListByColumns(columns);
    scope.setLocal("messageList", jpressAddonMessageList);
    renderBody(env, scope, writer);
}
 
Example #28
Source File: ShiroHasAnyPermissionDirective.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {
    if (getSubject() != null && ArrayUtil.isNotEmpty(exprList.getExprArray())) {
        for (Expr expr : exprList.getExprArray()) {
            if (getSubject().isPermitted(expr.eval(scope).toString())) {
                renderBody(env, scope, writer);
                break;
            }
        }
    }
}
 
Example #29
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 #30
Source File: ShiroGuestDirective.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public void onRender(Env env, Scope scope, Writer writer) {

    if (getSubject() == null || getSubject().getPrincipal() == null) {
        renderBody(env, scope, writer);
    }

}