其功能是:在首页 可以随意选取一个帖子,点击其标题,就可以打开可以详细显示其帖子的页面
在DiscussPostMapper 里面,定义selectDiscussPostById方法:
DiscussPost selectDiscussPostById(int id);//根据帖子id,查询出帖子详细信息。因为返回的是一个帖子,所以返回类型为DiscussPost
然后打开DiscussPostmapper,加入一个select
<select id="selectDiscussPostById" resultType="DiscussPost">
select <include refid="selectFields"></include>
from discuss_post
where id = #{id}
</select>
下面在业务层中也增加一个业务层方法:
public DiscussPost findDiscussPostById(int id) {
return discussPostMapper.selectDiscussPostById(id);
}
最后处理表现层逻辑:
@RequestMapping(path = "/detail/{discussPostId}", method = RequestMethod.GET)//声明路径。该方法要返回的是模板,即 discussdetail.html。所以不需要写ResponseBody
public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page) {//返回String是模板路径
DiscussPost post = discussPostService.findDiscussPostById(discussPostId);// 查询帖子
model.addAttribute("post", post);//传给模板
// 作者(要传回去的是头像和用户名字,而不是UserId)
User user = userService.findUserById(post.getUserId());
model.addAttribute("user", user);
接下来打开首页index.html
编辑如下:
<a th:href="@{|/discuss/detail/${map.post.id}|}" th:utext="${map.post.title}">备战春招,面试刷题跟他复习,一个月全搞定!</a>
接下来打开discussdetail.html
作者头像一开始是写死的,我们改为
<img th:src="${user.headerUrl}" class="align-self-start mr-4 rounded-circle user-header" alt="用户头像" >
把其改为
发布于 <b th:text="${#dates.format(post.createTime,'yyyy-MM-dd HH:mm:ss')}">2019-04-15 15:32:18</b>
还改了挺多,等到完全编辑好以后,再整段贴上来
测试:
打开这个帖子:
在浏览器右下角可以看到 当前路径
这个帖子做了敏感词过滤