spring@getmapping中的条件检查

bkhjykvo  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(242)

这是关于Spring Boot的,我的问题是。是否可以根据控制器请求方法中的某些条件呈现不同的视图。例如-

@GetMapping("/markSheet/{id}")
public String markSheet(@PathVariable("id") Long id, 
Model model) {

    //some code

    if(students.getMarkSet()!=null && students.getMarkSet().size!=0){            

           marksDtoObj.setMarkSet(students.getMarkSet());
           model.addAttribute(marksDtoObj);
           return "update";              

        }

       else
      // more code

      return "insertMarkSheet";
  }

update:- i 如果数据库中没有学生成绩的数据,我想向用户展示insert页面,如果数据库中有关于学生成绩的数据,我想向他展示一个更新页面。我想用单一的方法来解决这个问题。我已经解决了,
答案是可以做到的。
我是这样做的

@GetMapping("/otherMarks/{id}")
    public ModelAndView addOtherMarks(@PathVariable("id") Long id, Model model){

        ModelAndView modelAndView = new ModelAndView();

        OtherMarksDto odto=new OtherMarksDto();
        Students students=studentService.getStudentById(id);
        odto.setId(id);
        odto.setName(students.getName());
        odto.setRollNumber(students.getRollNumber());
        odto.setClassSection(students.getClassSection());

        if(students.getOtherMarksSet().isEmpty()!=true){

            odto.setOtherMarksSet(students.getOtherMarksSet());
            for(OtherMarks m:students.getOtherMarksSet()){
                odto.setMid(m.getId());
            }

            modelAndView.setViewName("editOtherMarks");
        }

    }

        else
            modelAndView.setViewName("otherMarks");

        model.addAttribute("marks", odto);
                    return modelAndView;
    }

现在我可以根据需要向用户显示这两个视图了,但是我在更新记录时遇到了一个新问题,它在服务方法save中显示了nullpointerexception,我想我应该问一个单独的问题

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题