ssm利用ajax实现用户点击关注|取消关注

ajax是直接上手,昨天才接触,本博文只是解决了关注功能,如有其他错误,请指出。

jsp前端代码

<div class="username" id="username">
                <h5>${article.username}
                    <c:if test="${article.userid  != userid}">
                        <button type="button"
                                class="layui-btn layui-btn-sm layui-btn-warm layui-btn-radius" id="addAttention" style="display: none">关注TA
                        </button>
                        <button type="button" class="layui-btn layui-btn-sm layui-btn-warm layui-btn-radius"
                                id="delAttention" style="display: none">取消关注
                        </button>
                    </c:if></h5>
                <a href="myHomepage?userid=${article.userid}">TA的个人主页</a>
                <input hidden id="beuserid" value="${article.userid}">
            </div>

ajax代码:

<script>
    $(document).ready(function () {
        beuserid = parseInt($("#beuserid").val());
        alert(beuserid);
        //是否关注了该帖子的用户
        $(function () {
            $.ajax({
                type: "Post",
                url: "isAttention/" + beuserid,
                contentType: "charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if (result.isAttention == 10) {
                        $('#addAttention').show();
                    }
                    if (result.isAttention == 11) {
                        $('#delAttention').show();
                    }
                },
                error: function (data) {
                    alert("是否关注--失败");
                }
            });
        });
        
        //点击关注
        $("#addAttention").click(function () {
            $('#addAttention').hide();
            $.ajax({
                type: "Post",
                url: "addAttention/" + beuserid,
                contentType: "charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if (result.resultCode == 200) {
                        $('#delAttention').show();
                    }
                },
                error: function (data) {
                    alert(data);
                }
            });
            return false;
        });
        //取消关注
        $("#delAttention").click(function () {
            $('#delAttention').hide()
            $.ajax({
                type: "Post",
                url: "deleteAttention/" + beuserid,
                contentType: "charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if (result.resultCode == 200) {
                        alert("200");
                        $('#addAttention').show();
                    }
                },
                error: function (err) {
                    alert(err);
                }
            });
            return false;
        });

    });
    
</script>

controller层代码:

//添加关注
    @RequestMapping("/addAttention/{beuserid}")
    @ResponseBody
    public Map addAttention(@PathVariable Integer beuserid, Attention attention, HttpServletRequest request, HttpSession session) {
        Map<Object, Object> map = new HashMap<>();
        //userid:主动关注者  beuserid:被关注者
        Integer userid = (Integer) session.getAttribute("userid");
        attention.setUserid(userid);
        attention.setBeuserid(beuserid);
        try {
            //新增关注
            attentionService.addAttention(attention);
            map.put("resultCode",200);
        }catch (Exception e){
            map.put("resultCode",404);
        }
        return map;
    }
    //取消关注
    @RequestMapping(value = "/deleteAttention/{beuserid}",method = RequestMethod.POST)
    @ResponseBody
    public Map deleteAttention(@PathVariable Integer beuserid, Attention attention, HttpServletRequest request, HttpSession session) {
        Map<Object, Object> map = new HashMap<>();
        //userid:主动关注者  beuserid:被关注者
        Integer userid = (Integer) session.getAttribute("userid");
        attentionService.deleteAttention(userid, beuserid);
        try {
            //取消关注
            attentionService.deleteAttention(userid,beuserid);
            map.put("resultCode", 200);
        } catch (Exception e) {
            map.put("resultCode", 404);
        }
        System.out.println("取消关注");
        return map;
    }
    //判断当前是否关注了该帖子的用户
    @RequestMapping("/isAttention/{beuserid}")
    @ResponseBody
    public Map isAttention(@PathVariable Integer beuserid, Attention attention, HttpServletRequest request, HttpSession session) {
        Map<Object, Object> map = new HashMap<>();
        //userid:主动关注者  beuserid:被关注者
        Integer userid = (Integer) session.getAttribute("userid");
        List<Attention> isAttention=attentionService.checkisAttention(userid,beuserid);
        if (isAttention.isEmpty()){//表示没关注
            map.put("isAttention", 10);
        }else {
            map.put("isAttention", 11);
        }
        System.out.println("是否关注");
        return map;
    }

其他代码就根据个人情况啦,只放重要的,说不定以后还要实现,类似功能,先记着。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值