前言
使用下面这些对象
@Autowired
private ArticleService articleService;
@Autowired
private CommentService commentService;
@Autowired
private UserService userService;
@Autowired
private TagService tagService;
@Autowired
private CategoryService categoryService;
一、 ArticleController实现的功能
- 文章详情页的显示
方法框架:
说明:value = "/article/{articleId}"接受url的参数传给Pathvariable,然后传给方法形参,这样下边通过文章ID实现功能返回的数据给前端
@RequestMapping(value = "/article/{articleId}")
public String getArticleDetailPage(@PathVariable("articleId") Integer articleId, Model model) {
//文章信息,分类,标签,作者,评论
//获得对应文章的对象中包含想要文章的所有信息
Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), articleId);
if (article == null) {
return "Home/Error/404";
}
//用户信息
User user = userService.getUserById(article.getArticleUserId());
article.setUser(user);
//文章信息
model.addAttribute("article", article);
//评论列表
List<Comment> commentList = commentService.listCommentByArticleId(articleId);
model.addAttribute("commentList", commentList);
//相关文章
List<Integer> categoryIds = articleService.listCategoryIdByArticleId(articleId);
List<Article> similarArticleList = articleService.listArticleByCategoryIds(categoryIds, 5);
model.addAttribute("similarArticleList", similarArticleList);
//猜你喜欢
List<Article> mostViewArticleList = articleService.listArticleByViewCount(5);
model.addAttribute("mostViewArticleList", mostViewArticleList);
//获取下一篇文章
Article afterArticle = articleService.getAfterArticle(articleId);
model.addAttribute("afterArticle", afterArticle);
//获取上一篇文章
Article preArticle = articleService.getPreArticle(articleId);
model.addAttribute("preArticle", preArticle);
//侧边栏
//标签列表显示
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);
return "Home/Page/articleDetail";
}
二、sidebar-1.jsp,articleDetail.jsp
1.通知栏那的导航
<nav class="breadcrumb">
<a class="crumbs" href="/">
<i class="fa fa-home"></i>首页
</a>
<c:choose>
<c:when test="${article.categoryList != null && article.categoryList.size() > 0}">
<c:forEach items="${article.categoryList}" var="c">
<i class="fa fa-angle-right"></i>
<a href="/category/${c.categoryId}">
${c.categoryName}
</a>
</c:forEach>
</c:when>
<c:otherwise>
<i class="fa fa-angle-right"></i>
<a>未分类</a>
</c:otherwise>
</c:choose>
<i class="fa fa-angle-right"></i>
正文
</nav>
2. 文章的显示
正文代码理解:大体还是后端发送过来的信息对象然后然后展示其中的内容,还有所属分类,以及i相关文章的显示,后端通过方法直接返回了相关文章列表,上一篇文章,下一篇文章