文章分类页-缓存分页设计

最后发布时间:2023-07-16 18:08:11 浏览量:

第一页


    /**
     * 生成该栏目下文章列表, 只展示文章列表
     * @param category
     */
    @Override
    public CategoryContentListDao convertArticleListBy(CategoryVO category) {
//        //生成分类列表,用于首页文章列表右侧展示
//        if(!TemplateUtil.componentsExist(category.getTemplateName())){
//                generateCategoryListHtml();
//        }
        Template template = templateService.findOptionalByEnName(category.getTemplateName());
        CategoryContentListDao categoryArticle = contentService.findCategoryContentBy(category,template, 0);
//        CategoryContentListDao categoryArticle = contentService.findCategoryContentBy(categoryService.covertToVo(category),template,0);

//        if(template.getTree()){
////            categoryArticle = articleService.findCategoryArticleBy(category);
//        }else {
//
//        }
        Map<String,Object> map = new HashMap<>();
        List<Template> templates = templateService.findByChild(template.getId());
        for (Template templateChild : templates){
            if(templateChild.getTemplateType().equals(TemplateType.ARTICLE_LIST)  && templateChild.getArticleSize()!=null && templateChild.getArticleSize()!=0){
                List<ContentVO> contents = categoryArticle.getContents();
                int size= templateChild.getArticleSize();
                CategoryContentListDao newCategoryArticle = new CategoryContentListDao();
                BeanUtils.copyProperties(categoryArticle, newCategoryArticle);
                if(contents.size()>size){
                    List<ContentVO> newContents = new ArrayList<>();
                    for (int i = 0;i<size;i++){
                        newContents.add(contents.get(i));
                    }

                    newCategoryArticle.setContents(newContents);
                }
                TemplateUtil.convertHtmlAndSave(category.getPath()+File.separator+templateChild.getEnName(),newCategoryArticle.getViewName(),newCategoryArticle, templateChild);
            }else {
                TemplateUtil.convertHtmlAndSave(category.getPath()+File.separator+templateChild.getEnName(),categoryArticle.getViewName(),categoryArticle, templateChild);
            }

            map.put(templateChild.getEnName(),category.getPath()+File.separator+templateChild.getEnName()+File.separator+categoryArticle.getViewName());
        }


//        log.debug("生成"+category.getName()+"分类下的第一个页面!");
//        String json = JSON.toJSON(categoryArticle).toString();
//        TemplateUtil.saveFile(category.getPath()+CMSUtils.getArticleListJs(),category.getViewName(),json,"json");




        map.put("view",categoryArticle);

        String html = TemplateUtil.convertHtmlAndSave(category.getPath(),categoryArticle.getViewName(),map, template);
        //生成文章列表组件,用于首页嵌入
//        String content = DocumentUtil.getDivContent(html, "#components");
//        if(StringUtils.isNotEmpty(content)){
//            TemplateUtil.saveFile(category.getPath()+CMSUtils.getFirstArticleList(),category.getViewName(),content);
//        }
//        if(categoryArticle.getChildren()!=null && categoryArticle.getChildren().size()!=0){
//            String categoryChildren = DocumentUtil.getDivContent(html, "#categoryChildren");
//            if(StringUtils.isNotEmpty(categoryChildren)){
//                TemplateUtil.saveFile(category.getPath()+CMSUtils.getCategoryChildren(),category.getViewName(),categoryChildren);
//            }
//        }



        /*生成只有标题的第一页文章列表*/
//        Template templateTitleList = templateService.findOptionalByEnName(CmsConst.CATEGORY_TITLE);
//        List<ContentVO> articleVOS = categoryArticle.getContents();
//        TemplateUtil.convertHtmlAndSave(CMSUtils.getFirstArticleTitleList(),categoryArticle.getViewName(),articleVOS, templateTitleList);


       /**
        * 生成父类的文章列表
        * **/
       if(category.getParentId()!=0){
           Category parentCategory = categoryService.findById(category.getParentId());
           convertArticleListBy(categoryService.covertToVo(parentCategory));

       }
        return categoryArticle;
    }

按钮分页

        if(viewName.contains("/")){
            int pos = viewName.lastIndexOf("/");
            String args1 = viewName.substring(0,pos);
            String args2 = viewName.substring(pos+1);
            pathArgs = new String[]{args1,args2};
            if(pathArgs.length<2){
                return false;
            }
            pathArgs = pathArgs[1].split("-");

            GenerateHtml generateHtml = CmsConfig.getBean(GenerateHtml.class);
            Method[] methods = generateHtml.getClass().getDeclaredMethods();
            for (Method method: methods){
                if(method.getName().equals(pathArgs[pathArgs.length-1])){
                    method.invoke(generateHtml,new Object[]{pathArgs});
                    return true;
                }
            }

        }
    public String page(String[] args){
//        File file = new File(CmsConst.WORK_DIR+"/html/"+page+".html");
        Category category = categoryService.findByViewName(args[0]);
        if(category==null){
            throw new ObjectException(args[0]+"不存在!");
        }
        String resultHtml = htmlService.convertArticleListBy(category,Integer.parseInt(args[1]));
        return resultHtml;
    }
    @Override
    public String convertArticleListBy(Category category, int page) {
        if(page<=0){
            return "Page is not exist!!";
        }
        Template template = templateService.findOptionalByEnName(category.getTemplateName());

        CategoryContentListDao categoryArticle = contentService.findCategoryContentBy(category,template, page-1);
//        Page<ArticleVO> articlePage = categoryArticle.getContents();
        if(page>categoryArticle.getTotalPages()){
            return "Page is not exist!!";
        }
        log.debug("生成"+category.getName()+"分类下的第["+page+"]个页面缓存!");
        String viewName =   category.getViewName()+"-"+String.valueOf(page)+"-page";
//            String viewName = String.valueOf(page);
        return TemplateUtil.convertHtmlAndSave(category.getPath(),viewName,categoryArticle,template);
    }

ajax分页

    @GetMapping(value = "/{path}/{viewName}-{page}-ajaxPage",produces={"text/html;charset=UTF-8;","application/json;"})
    @Anonymous
    @ResponseBody
    public String  contentCategoryPage(@PathVariable("viewName") String viewName,
                                       @PathVariable("path") String path,
                                       @PathVariable("page") Integer page){
        if(page<=0){
            return "Page is not exist!!";
        }
        path = "html/"+path;
        String viewNameStr = viewName+"-"+page+"-page";
        if(TemplateUtil.checkFileExist(path,viewNameStr)){
            return TemplateUtil.openFile(path,viewNameStr);
        }


        Category category = categoryService.findByViewName(viewName);
        return htmlService.convertArticleListBy(category,page);

    }