3.11 帖子详情

其功能是:在首页 可以随意选取一个帖子,点击其标题,就可以打开可以详细显示其帖子的页面

在这里插入图片描述
在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>

还改了挺多,等到完全编辑好以后,再整段贴上来

测试:

打开这个帖子:
在这里插入图片描述
在浏览器右下角可以看到 当前路径
在这里插入图片描述
在这里插入图片描述
这个帖子做了敏感词过滤
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值