Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何阻止您自己的通知_Java_Spring - Fatal编程技术网

Java 如何阻止您自己的通知

Java 如何阻止您自己的通知,java,spring,Java,Spring,我正在努力学习如何从一个可用的来源建立一个论坛。然而,我的问题是回复评论和回答问题的功能。当我回答我的问题或评论时,通知我“vkhacbao回答了vkhacbao的问题”(vkhacbao是我登录的帐户)。如何阻止或设置条件,以便在我发表评论时不会将其发送给我自己。请帮帮我,我尝试了三天,但都没有成功。非常感谢 代码: CommentController.java @Controller public class CommentController { @Resource private Use

我正在努力学习如何从一个可用的来源建立一个论坛。然而,我的问题是回复评论和回答问题的功能。当我回答我的问题或评论时,通知我“vkhacbao回答了vkhacbao的问题”(vkhacbao是我登录的帐户)。如何阻止或设置条件,以便在我发表评论时不会将其发送给我自己。请帮帮我,我尝试了三天,但都没有成功。非常感谢 代码:

CommentController.java

@Controller
public class CommentController {
@Resource
private UserMapper userMapper;
@Resource
private CommentMapper commentMapper;
@Resource
private QuestionMapper questionMapper;
@Resource
private NotificationMapper notificationMapper;

@ResponseBody
@RequestMapping(value = "/comment",method = RequestMethod.POST)
public Object post(@RequestBody CommentCreateDto commentCreateDto,
                   HttpServletRequest request){
    //把User写进session
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return "login";
    }
    User user = null;
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("token")) {
            String token = cookie.getValue();
            user = userMapper.findBytoken(token);
            if (user != null) {
                request.getSession().setAttribute("user", user);
                //获取未读的消息数量
                int unreadnum=notificationMapper.getunreadcount(user.getId());
                request.getSession().setAttribute("unreadnum",unreadnum);
            }
            break;
        }
    }
    //把评论插入数据库
    Comment comment=new Comment();
    comment.setParent_id(commentCreateDto.getParent_id());
    comment.setContent(commentCreateDto.getContent());
    comment.setType(commentCreateDto.getType());
    comment.setCreatetime(System.currentTimeMillis());
    comment.setCommentor(user.getId());
    commentMapper.insert(comment);

    if (commentCreateDto.getType()==2){
        //把回复评论的通知插入数据库
        Notification notification=new Notification();

        notification.setNotifier(comment.getCommentor());

        Comment comment2=commentMapper.getparentbyid(commentCreateDto.getParent_id());
        notification.setReceiver(comment2.getCommentor());
        notification.setOuterid(commentCreateDto.getParent_id());
        notification.setType(notificationEnum.NOTIFICATION_COMMENT.getType());
        notification.setCreatetime(System.currentTimeMillis());
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notificationMapper.inserts(notification);

        //增加评论数
        commentMapper.updatecommentcount(commentCreateDto.getParent_id());
        
    }
        else {
        //把回复问题的通知插入数据库
        Question question=questionMapper.getbyId(commentCreateDto.getParent_id());
        Notification notification=new Notification();
        notification.setNotifier(user.getId());
        notification.setReceiver(question.getCreateid());
        notification.setOuterid(commentCreateDto.getParent_id());
        notification.setType(notificationEnum.NOTIFICATION_QUESTION.getType());
        notification.setCreatetime(System.currentTimeMillis());
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notificationMapper.inserts(notification);
        //增加问题回复量
        questionMapper.updatecomment(commentCreateDto.getParent_id());
    }
    ResultDto resultDto=new ResultDto();
    return resultDto.success();
}

@ResponseBody
@RequestMapping(value = "/comment/{id}",method = RequestMethod.GET)
public ResultDto<List<CommentDto>> comments(@PathVariable(name = "id") int id,
                                            HttpServletRequest request){
    //查找type=2,即是回复评论的评论
    List<Comment> comments = commentMapper.getCommentByid(id,2);
    List<CommentDto> commentDto=new ArrayList<>();
    //找到User
    Cookie[] cookies = request.getCookies();
    User user = null;
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("token")) {
            String token = cookie.getValue();
            user = userMapper.findBytoken(token);
            break;
        }
    }
    //把二级评论和对应的User写进每个CommentDto集合中
    for (Comment comment:comments){
        CommentDto dto=new CommentDto();
        BeanUtils.copyProperties(comment,dto);
        dto.setUser(user);
        commentDto.add(dto);
    }
    ResultDto resultDto=new ResultDto();
    //返回数据给前端
    return resultDto.success(commentDto);
}

 }
@Controller
 public class NotificationController {

@Resource
private NotificationMapper notificationMapper;
@Resource
private CommentMapper commentMapper;

@GetMapping("/notification/{action}")
public String notification(@PathVariable("action")int id,
                           HttpServletRequest request){
    //将通知设置为已读
    notificationMapper.updatestatus(id);
    //获取type,检验是回复评论还是回复问题
    int type=notificationMapper.gettypebyid(id);
    int outerid=notificationMapper.getouteridbyid(id);
    int questionid;
    if(type== notificationEnum.NOTIFICATION_QUESTION.getType()){
        questionid=outerid;
    }else {
        questionid=commentMapper.getparentidbyid(id);
    }
    return "redirect:/question/"+questionid;
}
 }
@Controller
 public class QuestionController {

@Resource
private QuestionService questionService;
@Resource
private UserMapper userMapper;
@Resource
private CommentService commentService;
@Resource
private NotificationMapper notificationMapper;

@GetMapping("/question/{id}")
public String question(@PathVariable(name = "id")int id,
                       Model model,
                       HttpServletRequest request){
    //查找cookies,观察是否有token存在
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return "login";
    }
    User user = null;
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("token")) {
            String token = cookie.getValue();
            user = userMapper.findBytoken(token);
            if (user != null) {
                request.getSession().setAttribute("user", user);
                //获取未读的消息数量
                int unreadnum=notificationMapper.getunreadcount(user.getId());
                request.getSession().setAttribute("unreadnum",unreadnum);
            }
            break;
        }
    }
    Questiondto questiondto=questionService.getbyid(id);
    //增加阅读数
    questionService.increaseview(id);
    model.addAttribute("questionDto",questiondto);
    //展示回复数据
    List<CommentDto> comments=commentService.getByid(id);
    model.addAttribute("comments",comments);
    //相关问题
    String[] tags=questiondto.getTag().split(",");
    StringBuilder msg=new StringBuilder();
    for (String tag:tags){
        msg.append(tag);
        msg.append("|");
    }
    String result=msg.substring(0,msg.length()-1);
    List<Question> relativequestion =questionService.getbytag(id,result);
    model.addAttribute("relativequestion",relativequestion);

    return "question";
}
}
QuestionController.java

@Controller
public class CommentController {
@Resource
private UserMapper userMapper;
@Resource
private CommentMapper commentMapper;
@Resource
private QuestionMapper questionMapper;
@Resource
private NotificationMapper notificationMapper;

@ResponseBody
@RequestMapping(value = "/comment",method = RequestMethod.POST)
public Object post(@RequestBody CommentCreateDto commentCreateDto,
                   HttpServletRequest request){
    //把User写进session
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return "login";
    }
    User user = null;
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("token")) {
            String token = cookie.getValue();
            user = userMapper.findBytoken(token);
            if (user != null) {
                request.getSession().setAttribute("user", user);
                //获取未读的消息数量
                int unreadnum=notificationMapper.getunreadcount(user.getId());
                request.getSession().setAttribute("unreadnum",unreadnum);
            }
            break;
        }
    }
    //把评论插入数据库
    Comment comment=new Comment();
    comment.setParent_id(commentCreateDto.getParent_id());
    comment.setContent(commentCreateDto.getContent());
    comment.setType(commentCreateDto.getType());
    comment.setCreatetime(System.currentTimeMillis());
    comment.setCommentor(user.getId());
    commentMapper.insert(comment);

    if (commentCreateDto.getType()==2){
        //把回复评论的通知插入数据库
        Notification notification=new Notification();

        notification.setNotifier(comment.getCommentor());

        Comment comment2=commentMapper.getparentbyid(commentCreateDto.getParent_id());
        notification.setReceiver(comment2.getCommentor());
        notification.setOuterid(commentCreateDto.getParent_id());
        notification.setType(notificationEnum.NOTIFICATION_COMMENT.getType());
        notification.setCreatetime(System.currentTimeMillis());
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notificationMapper.inserts(notification);

        //增加评论数
        commentMapper.updatecommentcount(commentCreateDto.getParent_id());
        
    }
        else {
        //把回复问题的通知插入数据库
        Question question=questionMapper.getbyId(commentCreateDto.getParent_id());
        Notification notification=new Notification();
        notification.setNotifier(user.getId());
        notification.setReceiver(question.getCreateid());
        notification.setOuterid(commentCreateDto.getParent_id());
        notification.setType(notificationEnum.NOTIFICATION_QUESTION.getType());
        notification.setCreatetime(System.currentTimeMillis());
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notificationMapper.inserts(notification);
        //增加问题回复量
        questionMapper.updatecomment(commentCreateDto.getParent_id());
    }
    ResultDto resultDto=new ResultDto();
    return resultDto.success();
}

@ResponseBody
@RequestMapping(value = "/comment/{id}",method = RequestMethod.GET)
public ResultDto<List<CommentDto>> comments(@PathVariable(name = "id") int id,
                                            HttpServletRequest request){
    //查找type=2,即是回复评论的评论
    List<Comment> comments = commentMapper.getCommentByid(id,2);
    List<CommentDto> commentDto=new ArrayList<>();
    //找到User
    Cookie[] cookies = request.getCookies();
    User user = null;
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("token")) {
            String token = cookie.getValue();
            user = userMapper.findBytoken(token);
            break;
        }
    }
    //把二级评论和对应的User写进每个CommentDto集合中
    for (Comment comment:comments){
        CommentDto dto=new CommentDto();
        BeanUtils.copyProperties(comment,dto);
        dto.setUser(user);
        commentDto.add(dto);
    }
    ResultDto resultDto=new ResultDto();
    //返回数据给前端
    return resultDto.success(commentDto);
}

 }
@Controller
 public class NotificationController {

@Resource
private NotificationMapper notificationMapper;
@Resource
private CommentMapper commentMapper;

@GetMapping("/notification/{action}")
public String notification(@PathVariable("action")int id,
                           HttpServletRequest request){
    //将通知设置为已读
    notificationMapper.updatestatus(id);
    //获取type,检验是回复评论还是回复问题
    int type=notificationMapper.gettypebyid(id);
    int outerid=notificationMapper.getouteridbyid(id);
    int questionid;
    if(type== notificationEnum.NOTIFICATION_QUESTION.getType()){
        questionid=outerid;
    }else {
        questionid=commentMapper.getparentidbyid(id);
    }
    return "redirect:/question/"+questionid;
}
 }
@Controller
 public class QuestionController {

@Resource
private QuestionService questionService;
@Resource
private UserMapper userMapper;
@Resource
private CommentService commentService;
@Resource
private NotificationMapper notificationMapper;

@GetMapping("/question/{id}")
public String question(@PathVariable(name = "id")int id,
                       Model model,
                       HttpServletRequest request){
    //查找cookies,观察是否有token存在
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return "login";
    }
    User user = null;
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("token")) {
            String token = cookie.getValue();
            user = userMapper.findBytoken(token);
            if (user != null) {
                request.getSession().setAttribute("user", user);
                //获取未读的消息数量
                int unreadnum=notificationMapper.getunreadcount(user.getId());
                request.getSession().setAttribute("unreadnum",unreadnum);
            }
            break;
        }
    }
    Questiondto questiondto=questionService.getbyid(id);
    //增加阅读数
    questionService.increaseview(id);
    model.addAttribute("questionDto",questiondto);
    //展示回复数据
    List<CommentDto> comments=commentService.getByid(id);
    model.addAttribute("comments",comments);
    //相关问题
    String[] tags=questiondto.getTag().split(",");
    StringBuilder msg=new StringBuilder();
    for (String tag:tags){
        msg.append(tag);
        msg.append("|");
    }
    String result=msg.substring(0,msg.length()-1);
    List<Question> relativequestion =questionService.getbytag(id,result);
    model.addAttribute("relativequestion",relativequestion);

    return "question";
}
}
@控制器
公共类问题控制器{
@资源
私人提问服务;
@资源
私有用户映射器用户映射器;
@资源
私人评论服务;
@资源
私人通知映射器通知映射器;
@GetMapping(“/question/{id}”)
公共字符串问题(@PathVariable(name=“id”)int-id,
模型,
HttpServletRequest(请求){
//查找饼干观察是否有代币存在
Cookie[]cookies=request.getCookies();
如果(cookies==null){
返回“登录”;
}
User=null;
用于(Cookie:cookies){
if(cookie.getName().equals(“令牌”)){
字符串标记=cookie.getValue();
user=userMapper.findBytoken(令牌);
如果(用户!=null){
request.getSession().setAttribute(“用户”,用户);
//获取未读的消息数量
int unreadnum=notificationMapper.getunreadcount(user.getId());
request.getSession().setAttribute(“unreadnum”,unreadnum);
}
打破
}
}
QuestiondToQuestiondTo=questionService.getbyid(id);
//增加阅读数
questionService.increaseview(id);
addAttribute(“questionDto”,questionDto);
//展示回复数据
List comments=commentService.getByid(id);
model.addAttribute(“注释”,注释);
//相关问题
String[]tags=questiondto.getTag().split(“,”);
StringBuilder msg=新的StringBuilder();
用于(字符串标记:标记){
msg.append(标签);
msg.append(“|”);
}
字符串结果=msg.substring(0,msg.length()-1);
List relativequestion=questionService.getbytag(id,结果);
model.addAttribute(“relativeequestion”,relativeequestion);
返回“问题”;
}
}

假设原始消息的所有者和评论者都在CommentController类中,则此部分为

if(commentCreateDto.getType()==2){
Comment comment2=commentMapper.getparentbyid(commentCreateDto.getParent_id());
如果(!comment2.getCommentor().equals(comment.getCommentor())){
通知通知=新通知();
notification.setNotifier(comment.getCommentor());
notification.setReceiver(comment2.getCommentor());
notification.setOuterid(commentCreateDto.getParent_id());
notification.setType(notificationEnum.notification_COMMENT.getType());
notification.setCreatetime(System.currentTimeMillis());
notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
通知映射器插入(通知);
updatecommentcount(commentCreateDto.getParent_id());
}
}

不幸的是,如果这对您不起作用,您需要学习更多关于语言和代码的知识,以确定在何处进行更改。

如果
comment2.getCommentor()
是收件人,而
comment.getCommentor()
是回复注释者(如果我正确阅读了您的类),你可以确定他们不相等。你能帮我重写命令吗?我是个新手,所以写得不好。谢谢请帮助我#Gryphon对他们来说,你只需要使用
equals
,比如
if(!comment2.getCommentor().equals(comment.getCommentor())){
如果两个值相同,则忽略下面的代码,但如果两个值不同,则将执行代码。我如何编写?您可以复制该代码并为我添加一个条件吗?我是初学者,所以不太好。对不起,非常感谢