Java Code Examples for com.blade.mvc.http.Request#attribute()

The following examples show how to use com.blade.mvc.http.Request#attribute() . 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: ThemeController.java    From tale with MIT License 6 votes vote down vote up
@GetRoute(value = "")
public String index(Request request) {
    // 读取主题
    String         themesDir  = AttachController.CLASSPATH + "templates/themes";
    File[]         themesFile = new File(themesDir).listFiles();
    List<ThemeDto> themes     = new ArrayList<>(themesFile.length);
    for (File f : themesFile) {
        if (f.isDirectory()) {
            ThemeDto themeDto = new ThemeDto(f.getName());
            if (Files.exists(Paths.get(f.getPath() + "/setting.html"))) {
                themeDto.setHasSetting(true);
            }
            themes.add(themeDto);
            try {
                Blade.me().addStatics("/templates/themes/" + f.getName() + "/screenshot.png");
            } catch (Exception e) {
            }
        }
    }
    request.attribute("current_theme", Commons.site_theme());
    request.attribute("themes", themes);
    return "admin/themes";
}
 
Example 2
Source File: IndexController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 自定义页面
 */
@CsrfToken(newToken = true)
@GetRoute(value = {"/:cid", "/:cid.html"})
public String page(@PathParam String cid, Request request) {
    Optional<Contents> contentsOptional = contentsService.getContents(cid);
    if (!contentsOptional.isPresent()) {
        return this.render_404();
    }
    Contents contents = contentsOptional.get();
    if (contents.getAllowComment()) {
        int cp = request.queryInt("cp", 1);
        request.attribute("cp", cp);
    }
    request.attribute("article", contents);
    Contents temp = new Contents();
    temp.setHits(contents.getHits() + 1);
    temp.update(contents.getCid());
    if (Types.ARTICLE.equals(contents.getType())) {
        return this.render("post");
    }
    if (Types.PAGE.equals(contents.getType())) {
        return this.render("page");
    }
    return this.render_404();
}
 
Example 3
Source File: CategoryController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 标签下文章分页
 */
@GetRoute(value = {"tag/:name/:page", "tag/:name/:page.html"})
public String tags(Request request, @PathParam String name, @PathParam int page, @Param(defaultValue = "12") int limit) {
    page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
    Metas metaDto = metasService.getMeta(Types.TAG, name);
    if (null == metaDto) {
        return this.render_404();
    }

    Page<Contents> contentsPage = contentsService.getArticles(metaDto.getMid(), page, limit);
    request.attribute("articles", contentsPage);
    request.attribute("meta", metaDto);
    request.attribute("type", "标签");
    request.attribute("keyword", name);
    request.attribute("is_tag", true);
    request.attribute("page_prefix", "/tag/" + name);

    return this.render("page-category");
}
 
Example 4
Source File: BaseWebHook.java    From tale with MIT License 6 votes vote down vote up
private boolean isRedirect(Request request, Response response) {
    Users  user = TaleUtils.getLoginUser();
    String uri  = request.uri();
    if (null == user) {
        Integer uid = TaleUtils.getCookieUid(request);
        if (null != uid) {
            user = new Users().find(uid);
            request.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
        }
    }
    if (uri.startsWith(TaleConst.ADMIN_URI) && !uri.startsWith(TaleConst.LOGIN_URI)) {
        if (null == user) {
            response.redirect(TaleConst.LOGIN_URI);
            return false;
        }
        request.attribute(TaleConst.PLUGINS_MENU_NAME, TaleConst.PLUGIN_MENUS);
    }
    return true;
}
 
Example 5
Source File: IndexController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 仪表盘
 */
@Route(value = {"/", "index"}, method = HttpMethod.GET)
public String index(Request request) {
    List<Comments> comments   = siteService.recentComments(5);
    List<Contents> contents   = siteService.getContens(Types.RECENT_ARTICLE, 5);
    Statistics     statistics = siteService.getStatistics();
    // 取最新的20条日志
    Page<Logs> logsPage = new Logs().page(1, 20, "id desc");
    List<Logs> logs     = logsPage.getRows();

    request.attribute("comments", comments);
    request.attribute("articles", contents);
    request.attribute("statistics", statistics);
    request.attribute("logs", logs);
    return "admin/index";
}
 
Example 6
Source File: DefaultExceptionHandler.java    From blade with Apache License 2.0 6 votes vote down vote up
protected void render500(Request request, Response response) {
    var blade   = WebContext.blade();
    var page500 = Optional.ofNullable(blade.environment().get(ENV_KEY_PAGE_500, null));

    if (page500.isPresent()) {
        this.renderPage(response, new ModelAndView(page500.get()));
    } else {
        if (blade.devMode()) {
            var htmlCreator = new HtmlCreator();
            htmlCreator.center("<h1>" + request.attribute("title") + "</h1>");
            htmlCreator.startP("message-header");
            htmlCreator.add("Request URI: " + request.uri());
            htmlCreator.startP("message-header");
            htmlCreator.add("Error Message: " + request.attribute("message"));
            htmlCreator.endP();
            if (null != request.attribute(VARIABLE_STACKTRACE)) {
                htmlCreator.startP("message-body");
                htmlCreator.add(request.attribute(VARIABLE_STACKTRACE).toString().replace("\n", "<br/>"));
                htmlCreator.endP();
            }
            response.html(htmlCreator.html());
        } else {
            response.html(INTERNAL_SERVER_ERROR_HTML);
        }
    }
}
 
Example 7
Source File: ArticleController.java    From tale with MIT License 5 votes vote down vote up
/**
 * 文章发布页面
 *
 * @param request
 * @return
 */
@GetRoute(value = "publish")
public String newArticle(Request request) {
    List<Metas> categories = metasService.getMetas(Types.CATEGORY);
    request.attribute("categories", categories);
    request.attribute(Types.ATTACH_URL, Commons.site_option(Types.ATTACH_URL, Commons.site_url()));
    return "admin/article_edit";
}
 
Example 8
Source File: IndexController.java    From tale with MIT License 5 votes vote down vote up
@GetRoute(value = {"search/:keyword/:page", "search/:keyword/:page.html"})
public String search(Request request, @PathParam String keyword, @PathParam int page, @Param(defaultValue = "12") int limit) {

    page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;

    Page<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH)
            .like("title", "%" + keyword + "%").page(page, limit, "created desc");

    request.attribute("articles", articles);

    request.attribute("type", "搜索");
    request.attribute("keyword", keyword);
    request.attribute("page_prefix", "/search/" + keyword);
    return this.render("page-category");
}
 
Example 9
Source File: IndexController.java    From tale with MIT License 5 votes vote down vote up
/**
 * 首页分页
 *
 * @param request
 * @param page
 * @param limit
 * @return
 */
@GetRoute(value = {"page/:page", "page/:page.html"})
public String index(Request request, @PathParam int page, @Param(defaultValue = "12") int limit) {
    page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
    if (page > 1) {
        this.title(request, "第" + page + "页");
    }
    request.attribute("page_num", page);
    request.attribute("limit", limit);
    request.attribute("is_home", true);
    request.attribute("page_prefix", "/page");
    return this.render("index");
}
 
Example 10
Source File: ArticleController.java    From tale with MIT License 5 votes vote down vote up
/**
 * 文章编辑页面
 *
 * @param cid
 * @param request
 * @return
 */
@GetRoute(value = "/:cid")
public String editArticle(@PathParam String cid, Request request) {
    Optional<Contents> contents = contentsService.getContents(cid);
    if (!contents.isPresent()) {
        return render_404();
    }
    request.attribute("contents", contents.get());
    List<Metas> categories = metasService.getMetas(Types.CATEGORY);
    request.attribute("categories", categories);
    request.attribute("active", "article");
    request.attribute(Types.ATTACH_URL, Commons.site_option(Types.ATTACH_URL, Commons.site_url()));
    return "admin/article_edit";
}
 
Example 11
Source File: InstallController.java    From tale with MIT License 5 votes vote down vote up
/**
 * 安装页
 *
 * @return
 */
@Route(value = "/", method = HttpMethod.GET)
public String index(Request request) {
    boolean existInstall = Files.exists(Paths.get(AttachController.CLASSPATH + "install.lock"));
    int allow_reinstall = TaleConst.OPTIONS.getInt("allow_install", 0);

    if (allow_reinstall == 1) {
        request.attribute("is_install", false);
    } else {
        request.attribute("is_install", existInstall);
    }
    return "install";
}
 
Example 12
Source File: CommentController.java    From tale with MIT License 5 votes vote down vote up
@GetRoute(value = "")
public String index(@Param(defaultValue = "1") int page,
                    @Param(defaultValue = "15") int limit, Request request) {
    Users users = this.user();

    Page<Comments> commentPage = new Comments().where("author_id", "<>", users.getUid()).page(page, limit);
    request.attribute("comments", commentPage);
    return "admin/comment_list";
}
 
Example 13
Source File: CategoryController.java    From tale with MIT License 5 votes vote down vote up
@Route(value = "", method = HttpMethod.GET)
public String index(Request request) {
    List<Metas>   categories = siteService.getMetas(Types.RECENT_META, Types.CATEGORY, TaleConst.MAX_POSTS);
    List<Metas> tags       = siteService.getMetas(Types.RECENT_META, Types.TAG, TaleConst.MAX_POSTS);
    request.attribute("categories", categories);
    request.attribute("tags", tags);
    return "admin/category";
}
 
Example 14
Source File: TemplateController.java    From tale with MIT License 5 votes vote down vote up
@Route(value = "", method = HttpMethod.GET)
public String index(Request request) {
    String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme();
    try {
        List<String> files = Files.list(Paths.get(themePath))
                .map(path -> path.getFileName().toString())
                .filter(path -> path.endsWith(".html"))
                .collect(Collectors.toList());

        List<String> partial = Files.list(Paths.get(themePath + File.separatorChar + "partial"))
                .map(path -> path.getFileName().toString())
                .filter(path -> path.endsWith(".html"))
                .map(fileName -> "partial/" + fileName)
                .collect(Collectors.toList());

        List<String> statics = Files.list(Paths.get(themePath + File.separatorChar + "static"))
                .map(path -> path.getFileName().toString())
                .filter(path -> path.endsWith(".js") || path.endsWith(".css"))
                .map(fileName -> "static/" + fileName)
                .collect(Collectors.toList());

        files.addAll(partial);
        files.addAll(statics);

        request.attribute("tpls", files);
    } catch (IOException e) {
        log.error("找不到模板路径");
    }
    return "admin/tpl_list";
}
 
Example 15
Source File: DefaultExceptionHandler.java    From blade with Apache License 2.0 5 votes vote down vote up
protected void handleException(Exception e, Request request, Response response) {
    log.error("", e);
    if (null == response) {
        return;
    }
    response.status(500);
    request.attribute("title", "500 Internal Server Error");
    request.attribute("message", e.getMessage());
    request.attribute("stackTrace", getStackTrace(e));
    this.render500(request, response);
}
 
Example 16
Source File: PageController.java    From tale with MIT License 5 votes vote down vote up
@Route(value = "/:cid", method = HttpMethod.GET)
public String editPage(@PathParam String cid, Request request) {
    Optional<Contents> contents = contentsService.getContents(cid);
    if (!contents.isPresent()) {
        return render_404();
    }
    request.attribute("contents", contents.get());
    request.attribute(Types.ATTACH_URL, Commons.site_option(Types.ATTACH_URL, Commons.site_url()));
    return "admin/page_edit";
}
 
Example 17
Source File: PageController.java    From tale with MIT License 4 votes vote down vote up
@Route(value = "", method = HttpMethod.GET)
public String index(Request request) {
    Page<Contents> contentsPage = new Contents().where("type", Types.PAGE).page(1, TaleConst.MAX_POSTS, "created desc");
    request.attribute("articles", contentsPage);
    return "admin/page_list";
}
 
Example 18
Source File: DefaultExceptionHandler.java    From blade with Apache License 2.0 4 votes vote down vote up
protected void handleBladeException(BladeException e, Request request, Response response) {
    var blade = WebContext.blade();
    response.status(e.getStatus());

    var modelAndView = new ModelAndView();
    modelAndView.add("title", e.getStatus() + " " + e.getName());
    modelAndView.add("message", e.getMessage());

    if (null != e.getCause()) {
        request.attribute(VARIABLE_STACKTRACE, getStackTrace(e));
    }

    if (e.getStatus() == InternalErrorException.STATUS) {
        log.error("", e);
        this.render500(request, response);
    }

    String paddingMethod = BladeCache.getPaddingMethod(request.method());

    if (e.getStatus() == NotFoundException.STATUS) {
        log404(log, paddingMethod, request.uri());

        if (request.isJsonRequest()) {
            response.json(RestResponse.fail(NotFoundException.STATUS, "Not Found [" + request.uri() + "]"));
        } else {
            var page404 = Optional.ofNullable(blade.environment().get(ENV_KEY_PAGE_404, null));
            if (page404.isPresent()) {
                modelAndView.setView(page404.get());
                renderPage(response, modelAndView);
                response.render(page404.get());
            } else {
                HtmlCreator htmlCreator = new HtmlCreator();
                htmlCreator.center("<h1>404 Not Found - " + request.uri() + "</h1>");
                htmlCreator.hr();
                response.html(htmlCreator.html());
            }
        }
    }

    if (e.getStatus() == MethodNotAllowedException.STATUS) {

        log405(log, paddingMethod, request.uri());

        if (request.isJsonRequest()) {
            response.json(RestResponse.fail(MethodNotAllowedException.STATUS, e.getMessage()));
        } else {
            response.text(e.getMessage());
        }
    }
}
 
Example 19
Source File: RouteExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@GetRoute("/template-output-test")
public void templateOutputTest(Request request, Response response) {
    request.attribute("name", "Blade");
    response.render("template-output-test.html");
}
 
Example 20
Source File: PageController.java    From tale with MIT License 4 votes vote down vote up
@Route(value = "new", method = HttpMethod.GET)
public String newPage(Request request) {
    request.attribute(Types.ATTACH_URL, Commons.site_option(Types.ATTACH_URL, Commons.site_url()));
    return "admin/page_edit";
}