cn.hutool.core.util.PageUtil Java Examples

The following examples show how to use cn.hutool.core.util.PageUtil. 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: FrontIndexController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 首页分页
 *
 * @param model model
 * @param page  当前页码
 * @param size  每页数量
 * @return 模板路径/themes/{theme}/index
 */
@GetMapping(value = "page/{page}")
public String index(Model model,
                    @PathVariable(value = "page") Integer page) {
    final Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    //默认显示10条
    int size = 10;
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
    }
    //所有文章数据,分页
    final Pageable pageable = PageRequest.of(page - 1, size, sort);
    final Page<Post> posts = postService.findPostByStatus(pageable);
    if (null == posts) {
        return this.renderNotFound();
    }
    final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
    model.addAttribute("is_index",true);
    model.addAttribute("posts", posts);
    model.addAttribute("rainbow", rainbow);
    return this.render("index");
}
 
Example #2
Source File: FrontTagController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 根据标签路径查询所有文章 分页
 *
 * @param model  model
 * @param tagUrl 标签路径
 * @param page   页码
 *
 * @return String
 */
@GetMapping(value = "{tagUrl}/page/{page}")
public String tags(Model model,
                   @PathVariable("tagUrl") String tagUrl,
                   @PathVariable("page") Integer page) {
    final Tag tag = tagService.findByTagUrl(tagUrl);
    if (null == tag) {
        return this.renderNotFound();
    }
    final Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    int size = 10;
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
    }
    final Pageable pageable = PageRequest.of(page - 1, size, sort);
    final Page<Post> posts = postService.findPostsByTags(tag, pageable);
    final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
    model.addAttribute("is_tags", true);
    model.addAttribute("posts", posts);
    model.addAttribute("rainbow", rainbow);
    model.addAttribute("tag", tag);
    return this.render("tag");
}
 
Example #3
Source File: FrontCategoryController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 根据分类目录查询所有文章 分页
 *
 * @param model   model
 * @param cateUrl 分类目录路径
 * @param page    页码
 *
 * @return String
 */
@GetMapping("{cateUrl}/page/{page}")
public String categories(Model model,
                         @PathVariable("cateUrl") String cateUrl,
                         @PathVariable("page") Integer page) {
    final Category category = categoryService.findByCateUrl(cateUrl);
    if (null == category) {
        return this.renderNotFound();
    }
    final Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    int size = 10;
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
    }
    final Pageable pageable = PageRequest.of(page - 1, size, sort);
    final Page<Post> posts = postService.findPostByCategories(category, pageable);
    final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
    model.addAttribute("is_categories", true);
    model.addAttribute("posts", posts);
    model.addAttribute("rainbow", rainbow);
    model.addAttribute("category", category);
    return this.render("category");
}
 
Example #4
Source File: FrontIndexController.java    From blog-sharon with Apache License 2.0 6 votes vote down vote up
/**
 * 首页分页
 *
 * @param model model
 * @param page  当前页码
 * @param size  每页数量
 * @return 模板路径/themes/{theme}/index
 */
@GetMapping(value = "page/{page}")
public String index(Model model,
                    @PathVariable(value = "page") Integer page) {
    Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    //默认显示10条
    int size = 10;
    //尝试加载设置选项,用于设置显示条数
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
    }
    //所有文章数据,分页
    Pageable pageable = PageRequest.of(page - 1, size, sort);
    Page<Post> posts = postService.findPostByStatus(pageable);
    if (null == posts) {
        return this.renderNotFound();
    }
    int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
    model.addAttribute("is_index",true);
    model.addAttribute("posts", posts);
    model.addAttribute("rainbow", rainbow);
    return this.render("index");
}
 
Example #5
Source File: FrontTagController.java    From blog-sharon with Apache License 2.0 6 votes vote down vote up
/**
 * 根据标签路径查询所有文章 分页
 *
 * @param model  model
 * @param tagUrl 标签路径
 * @param page   页码
 * @return String
 */
@GetMapping(value = "{tagUrl}/page/{page}")
public String tags(Model model,
                   @PathVariable("tagUrl") String tagUrl,
                   @PathVariable("page") Integer page) {
    Tag tag = tagService.findByTagUrl(tagUrl);
    if(null==tag){
        return this.renderNotFound();
    }
    Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    Integer size = 10;
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
    }
    Pageable pageable = PageRequest.of(page - 1, size, sort);
    Page<Post> posts = postService.findPostsByTags(tag, pageable);
    int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
    model.addAttribute("is_tags",true);
    model.addAttribute("posts", posts);
    model.addAttribute("rainbow", rainbow);
    model.addAttribute("tag", tag);
    return this.render("tag");
}
 
Example #6
Source File: FrontCategoryController.java    From blog-sharon with Apache License 2.0 6 votes vote down vote up
/**
 * 根据分类目录查询所有文章 分页
 *
 * @param model   model
 * @param cateUrl 分类目录路径
 * @param page    页码
 * @return String
 */
@GetMapping("{cateUrl}/page/{page}")
public String categories(Model model,
                         @PathVariable("cateUrl") String cateUrl,
                         @PathVariable("page") Integer page) {
    Category category = categoryService.findByCateUrl(cateUrl);
    if (null == category) {
        return this.renderNotFound();
    }
    Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    Integer size = 10;
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
    }
    Pageable pageable = PageRequest.of(page - 1, size, sort);
    Page<Post> posts = postService.findPostByCategories(category, pageable);
    int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
    model.addAttribute("is_categories",true);
    model.addAttribute("posts", posts);
    model.addAttribute("rainbow", rainbow);
    model.addAttribute("category", category);
    return this.render("category");
}
 
Example #7
Source File: ToolTagDirective.java    From halo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);

    if (params.containsKey(HaloConst.METHOD_KEY)) {
        String method = params.get(HaloConst.METHOD_KEY).toString();
        switch (method) {
            case "rainbowPage":
                int page = Integer.parseInt(params.get("page").toString());
                int total = Integer.parseInt(params.get("total").toString());
                int display = Integer.parseInt(params.get("display").toString());
                env.setVariable("numbers", builder.build().wrap(PageUtil.rainbow(page, total, display)));
                break;
            case "random":
                int min = Integer.parseInt(params.get("min").toString());
                int max = Integer.parseInt(params.get("max").toString());
                env.setVariable("number", builder.build().wrap(RandomUtil.randomInt(min, max)));
                break;
            default:
                break;
        }
    }
    body.render(env.getOut());
}
 
Example #8
Source File: CommentServiceImpl.java    From SENS with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CommentPageDTO findCommentPageByPostId(Long postId, Integer pageNumber) {
    List<Comment> comments = this.findCommentsByPostAndCommentStatus(postId, CommentStatusEnum.PUBLISHED.getCode());
    //默认显示10条
    Integer size = 10;
    //获取每页评论条数
    if (!StringUtils.isBlank(SensConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()))) {
        size = Integer.parseInt(SensConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
    }
    //评论分页
    ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments), pageNumber, size);
    int[] rainbow = PageUtil.rainbow(pageNumber, commentsPage.getTotalPage(), 10);
    return new CommentPageDTO(commentsPage, rainbow);
}
 
Example #9
Source File: FrontPageController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 渲染自定义页面
 *
 * @param postUrl 页面路径
 * @param model   model
 *
 * @return 模板路径/themes/{theme}/post
 */
@GetMapping(value = "/p/{postUrl}")
public String getPage(@PathVariable(value = "postUrl") String postUrl,
                      @RequestParam(value = "cp", defaultValue = "1") Integer cp,
                      Model model) {
    final Post post = postService.findByPostUrl(postUrl, PostTypeEnum.POST_TYPE_PAGE.getDesc());
    if (null == post || !post.getPostStatus().equals(PostStatusEnum.PUBLISHED.getCode())) {
        return this.renderNotFound();
    }
    List<Comment> comments = null;
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || HaloConst.OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
        comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatusEnum.PUBLISHED.getCode());
    } else {
        comments = commentService.findCommentsByPostAndCommentStatusNot(post, CommentStatusEnum.RECYCLE.getCode());
    }
    //默认显示10条
    int size = 10;
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
    }
    //评论分页
    final ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments), cp, size);
    final int[] rainbow = PageUtil.rainbow(cp, commentsPage.getTotalPage(), 3);
    model.addAttribute("is_page", true);
    model.addAttribute("post", post);
    model.addAttribute("comments", commentsPage);
    model.addAttribute("commentsCount", comments.size());
    model.addAttribute("rainbow", rainbow);
    postService.cacheViews(post.getPostId());

    //如果设置了自定义模板,则渲染自定义模板
    if (StrUtil.isNotEmpty(post.getCustomTpl())) {
        return this.render(post.getCustomTpl());
    }
    return this.render("page");
}
 
Example #10
Source File: RedisTokensServiceImpl.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public PageResult<TokenVo> listTokens(Map<String, Object> params, String clientId) {
    Integer page = MapUtils.getInteger(params, "page");
    Integer limit = MapUtils.getInteger(params, "limit");
    int[] startEnds = PageUtil.transToStartEnd(page, limit);
    //根据请求参数生成redis的key
    String redisKey = getRedisKey(params, clientId);
    long size = redisRepository.length(redisKey);
    List<TokenVo> result = new ArrayList<>(limit);
    //查询token集合
    List<Object> tokenObjs = redisRepository.getList(redisKey, startEnds[0], startEnds[1]-1);
    if (tokenObjs != null) {
        for (Object obj : tokenObjs) {
            DefaultOAuth2AccessToken accessToken = (DefaultOAuth2AccessToken)obj;
            //构造token对象
            TokenVo tokenVo = new TokenVo();
            tokenVo.setTokenValue(accessToken.getValue());
            tokenVo.setExpiration(accessToken.getExpiration());

            //获取用户信息
            Object authObj = redisRepository.get(SecurityConstants.REDIS_TOKEN_AUTH + accessToken.getValue());
            OAuth2Authentication authentication = (OAuth2Authentication)authObj;
            if (authentication != null) {
                OAuth2Request request = authentication.getOAuth2Request();
                tokenVo.setUsername(authentication.getName());
                tokenVo.setClientId(request.getClientId());
                tokenVo.setGrantType(request.getGrantType());
            }

            result.add(tokenVo);
        }
    }
    return PageResult.<TokenVo>builder().data(result).code(0).count(size).build();
}
 
Example #11
Source File: PageResult.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 构造器
 *
 * @param query 查询参数
 * @param total 总记录数
 * @param list  当前页数据
 */
public <Q extends PageQuery> PageResult(Q query, int total, List<T> list) {
	this.currentPage = query.getCurrentPage();
	this.pageSize = query.getPageSize();
	this.total = (long) total;
	this.list = list;
	this.totalPage = PageUtil.totalPage(total, pageSize);
}
 
Example #12
Source File: FrontPageController.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
/**
 * 渲染自定义页面
 *
 * @param postUrl 页面路径
 * @param model   model
 * @return 模板路径/themes/{theme}/post
 */
@GetMapping(value = "/p/{postUrl}")
public String getPage(@PathVariable(value = "postUrl") String postUrl,
                      @RequestParam(value = "cp", defaultValue = "1") Integer cp,
                      Model model) {
    Post post = postService.findByPostUrl(postUrl, PostTypeEnum.POST_TYPE_PAGE.getDesc());
    if (null == post) {
        return this.renderNotFound();
    }
    List<Comment> comments = null;
    if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || HaloConst.OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
        comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatusEnum.PUBLISHED.getCode());
    } else {
        comments = commentService.findCommentsByPostAndCommentStatusNot(post, CommentStatusEnum.RECYCLE.getCode());
    }
    //默认显示10条
    Integer size = 10;
    //获取每页评论条数
    if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
    }
    //评论分页
    ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments), cp, size);
    int[] rainbow = PageUtil.rainbow(cp, commentsPage.getTotalPage(), 3);
    model.addAttribute("is_page", true);
    model.addAttribute("post", post);
    model.addAttribute("comments", commentsPage);
    model.addAttribute("commentsCount", comments.size());
    model.addAttribute("rainbow", rainbow);
    postService.cacheViews(post.getPostId());

    //如果设置了自定义模板,则渲染自定义模板
    if (StrUtil.isNotEmpty(post.getCustomTpl())) {
        return this.render(post.getCustomTpl());
    }
    return this.render("page");
}
 
Example #13
Source File: FrontNoticeController.java    From SENS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 渲染公告详情
 *
 * @param postId 公告Id
 * @param model  model
 * @return 模板路径/themes/{theme}/notice
 */
@GetMapping(value = {"{postId}"})
public String getNotice(@PathVariable(value = "postId") Long postId,
                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                        Model model) {
    //1、查询公告
    Post post = postService.get(postId);


    if (null == post || !post.getPostStatus().equals(PostStatusEnum.PUBLISHED.getCode())) {
        return this.renderNotFound();
    }

    //2、上一篇下一篇
    Post beforePost = postService.findPreciousPost(post.getId(), PostTypeEnum.POST_TYPE_NOTICE.getValue());
    Post afterPost = postService.findNextPost(post.getId(), PostTypeEnum.POST_TYPE_NOTICE.getValue());
    model.addAttribute("beforePost", beforePost);
    model.addAttribute("afterPost", afterPost);

    //3、评论列表
    List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post.getId(), CommentStatusEnum.PUBLISHED.getCode());

    //默认显示10条
    Integer size = 10;
    //获取每页评论条数
    if (!StringUtils.isBlank(SensConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()))) {
        size = Integer.parseInt(SensConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
    }
    //评论分页
    ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments), page, size);
    int[] rainbow = PageUtil.rainbow(page, commentsPage.getTotalPage(), 10);

    //5.公告访问量
    postService.updatePostView(post.getId());

    //6、作者
    User user = userService.get(post.getUserId());
    post.setUser(user);

    //7、是否是作者
    User loginUser = getLoginUser();
    if (loginUser != null && Objects.equals(loginUser.getId(), post.getUserId())) {
        model.addAttribute("isAuthor", Boolean.TRUE);
    }

    model.addAttribute("post", post);
    model.addAttribute("comments", commentsPage);
    model.addAttribute("commentCount", comments.size());
    model.addAttribute("rainbow", rainbow);

    //侧边栏
    model.addAttribute("sidebarType", SidebarTypeEnum.NOTICE.getValue());
    model.addAttribute("noticeList", postService.findAllPosts(PostTypeEnum.POST_TYPE_NOTICE.getValue()));
    return this.render("notice");
}
 
Example #14
Source File: PageAdapter.java    From mall4j with GNU Affero General Public License v3.0 4 votes vote down vote up
public PageAdapter(Page page) {
    int[] startEnd = PageUtil.transToStartEnd((int) page.getCurrent(), (int) page.getSize());
    this.begin = startEnd[0];
    this.size = (int)page.getSize();
}
 
Example #15
Source File: PageResult.java    From magic-starter with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * 构造器
 *
 * @param currentPage 当前页
 * @param pageSize    每页条数
 * @param total       总记录数
 * @param list        当前页数据
 */
public PageResult(int currentPage, int pageSize, int total, List<T> list) {
	this.currentPage = currentPage;
	this.pageSize = pageSize;
	this.total = (long) total;
	this.list = list;
	this.totalPage = PageUtil.totalPage(total, pageSize);
}
 
Example #16
Source File: PageResult.java    From magic-starter with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 构造分页对象
 *
 * @param query 查询参数
 * @param total 总记录数
 * @param list  当前页数据
 * @param <Q>   泛型
 * @param <T>   泛型
 * @return 分页对象
 */
public static <Q extends PageQuery, T> PageResult<T> of(Q query, Number total, List<T> list) {
	return new PageResult<>(query.getCurrentPage(), query.getPageSize(), PageUtil.totalPage(total.intValue(), query.getPageSize()), total.longValue(), list);
}