博客项目实现文章评论功能(重点是评论回复)

本文详述了作者在博客项目中实现文章评论功能的过程,特别是评论回复功能的实现。评论部分使用textarea,后台进行字符转义防止XSS攻击。评论显示基于文章,采用Java实现分页和排序。评论回复借鉴博客园的逻辑,通过JavaScript处理[quote]标签,实现在前端展示引用内容。
摘要由CSDN通过智能技术生成

我开发的博客网站的地址:http://118.89.29.170/RiXiang_blog/

博客项目代码github:https://github.com/SonnAdolf/sonne_blog

有了我的已成型的项目和代码,可以更容易理解这篇文章。

 

本篇文章记录下自己博客项目评论功能实现的全过程,重点其实是评论回复功能。

 

【一,写评论】

写评论部分我没有使用富文本编辑器,只是单纯地使用了textarea标签,所以后台不需要作html标签的白名单检验(关于防范xss攻击,可以看我之前的一篇文章http://www.cnblogs.com/rixiang/p/6239464.html),只需要将所有<和>字符作转义即可。

提交到后台需要做的处理:必要的校验,存储。然后生成消息。消息会在用户个人空间消息中心显示。提示用户有新的评论。

数据库存储方面,评论与文章的关系是双向多对一。设置懒加载和级联删除。

写评论部分就这些,没什么好说的。

 

【二,评论显示】

评论的显示是基于文章的。也就是说在点击、查看一篇文章的同时,在该文章下面显示对这篇文章的所有评论。

上面提到评论和文章是多对一的关系,可知,查询到文章即可查询到该文章的所有评论。也正因此,且鉴于自己博客评论数并不很多情况,对于评论的分页我没有采用数据库查询层的分页方法,而是用java写了分页、排序。我并不确定最好的实现。

    /*
     * Select the article by the id, and show it at the jsp page.
     * 
     * @param HttpServletRequest request, Integer id, Model model
     * 
     * @return the jsp page
     */
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String showFromMainPage(HttpServletRequest request, Integer id,
            Integer currentPage, Model model) throws Exception {
        if (null == id) {
            return "error";
        }
        Article article = articleService.find(id, Article.class);

        // click the link, then read_times ++
        article.setRead_times(article.getRead_times() + 1);
        articleService.update(article);

        article = getArticleOfContentByUrl(article);
        // sort the comments
        List<Comment> comments = commentService.sort(article.getComments());
        String username = userService.getUsernameFromSession(request);
        model.addAttribute("article", article);
        model.addAttribute("username", username);
        model.addAttribute("article_id", id);

        
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值