package com.liuyanzhao.ssm.blog.controller.home; import com.github.pagehelper.PageInfo; import com.liuyanzhao.ssm.blog.enums.ArticleStatus; import com.liuyanzhao.ssm.blog.entity.Article; import com.liuyanzhao.ssm.blog.entity.Category; import com.liuyanzhao.ssm.blog.entity.Tag; import com.liuyanzhao.ssm.blog.service.ArticleService; import com.liuyanzhao.ssm.blog.service.CategoryService; import com.liuyanzhao.ssm.blog.service.TagService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; /** * 文章分类目录的controller * * @author 言曌 * @date 2017/8/24 */ @Controller public class CategoryController { @Autowired private CategoryService categoryService; @Autowired private ArticleService articleService; @Autowired private TagService tagService; /** * 根据分类查询文章 * * @param cateId 分类ID * @return 模板 */ @RequestMapping("/category/{cateId}") public String getArticleListByCategory(@PathVariable("cateId") Integer cateId, @RequestParam(required = false, defaultValue = "1") Integer pageIndex, @RequestParam(required = false, defaultValue = "10") Integer pageSize, Model model) { //该分类信息 Category category = categoryService.getCategoryById(cateId); if (category == null) { return "redirect:/404"; } model.addAttribute("category", category); //文章列表 HashMap criteria = new HashMap<>(2); criteria.put("categoryId", cateId); criteria.put("status", ArticleStatus.PUBLISH.getValue()); PageInfo
articlePageInfo = articleService.pageArticle(pageIndex, pageSize, criteria); model.addAttribute("pageInfo", articlePageInfo); //侧边栏 //标签列表显示 List allTagList = tagService.listTag(); model.addAttribute("allTagList", allTagList); //获得随机文章 List
randomArticleList = articleService.listRandomArticle(8); model.addAttribute("randomArticleList", randomArticleList); //获得热评文章 List
mostCommentArticleList = articleService.listArticleByCommentCount(8); model.addAttribute("mostCommentArticleList", mostCommentArticleList); model.addAttribute("pageUrlPrefix", "/category/"+cateId+"?pageIndex"); return "Home/Page/articleListByCategory"; } }