仿牛客网社区项目(十三)帖子详情

帖子详情

  • DiscussPostMapper

  • DiscussPostService

  • DiscussPostController

  • index.html

    • 在帖子标题上增加访问详情页面的链接
  • discuss-detail.html

    • 处理静态资源的访问路径
    • 复用index.html的header区域
    • 显示标题、作者、发布时间、帖子正文等内容

根据帖子的主键id查询贴子的详情内容。

DiscussPostMapper接口

DiscussPost selectDiscussPostById(int id);

discusspost-mapper.xml

<select id="selectDiscussPostById" resultType="DiscussPost">
    select <include refid="selectFields"></include>
    from discuss_post
    where id = #{id}
</select>

DiscussPostService

public DiscussPost findDiscussPostById(int id) {
    return discussPostMapper.selectDiscussPostById(id);
}

DiscussPostController

@RequestMapping(path = "/detail/{discussPostId}", method = RequestMethod.GET)
public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model) {
    // 帖子
    DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
    model.addAttribute("post", post);
    // 作者
    User user = userService.findUserById(post.getUserId());
    model.addAttribute("user", user);

    return "/site/discuss-detail";
}

index.html,改超链接。| |是常量和变量拼接的。取帖子主键用${map.post.id}

th:href="@{|/discuss/detail/${map.post.id}|}"

discuss-detail.html,改动态标题、头像、用户名、发帖时间、帖子内容

th:utext="${post.title}"
th:src="${user.headerUrl}"
th:utext="${user.username}"
th:text="${#dates.format(post.createTime,'yyyy-MM-dd HH:mm:ss')}"
th:utext="${post.content}"
posted @ 2022-04-30 15:41  卷皇  阅读(362)  评论(0)    收藏  举报