-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CategoryController.java
executable file
·83 lines (66 loc) · 2.7 KB
/
CategoryController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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<String, Object> criteria = new HashMap<>(2);
criteria.put("categoryId", cateId);
criteria.put("status", ArticleStatus.PUBLISH.getValue());
PageInfo<Article> articlePageInfo = articleService.pageArticle(pageIndex, pageSize, criteria);
model.addAttribute("pageInfo", articlePageInfo);
//侧边栏
//标签列表显示
List<Tag> allTagList = tagService.listTag();
model.addAttribute("allTagList", allTagList);
//获得随机文章
List<Article> randomArticleList = articleService.listRandomArticle(8);
model.addAttribute("randomArticleList", randomArticleList);
//获得热评文章
List<Article> mostCommentArticleList = articleService.listArticleByCommentCount(8);
model.addAttribute("mostCommentArticleList", mostCommentArticleList);
model.addAttribute("pageUrlPrefix", "/category/"+pageIndex+"?pageIndex");
return "Home/Page/articleListByCategory";
}
}