0

this is regarding spring boot, my question is. Is it possible to present different views based upon some condition in controller's Request Method. For example -

@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 wanted to present users with Insert page if there was no previous data of Students's Marks in DB, and If there was any Data regarding Students Marks is in DB I wanted to present him an update page. and I wanted to do it with single RequestMethod.I have solved it,

the answer is Yes it can be done.

I did it this way

  @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;
    }

Now I am able to present to user both the views as per the requirement, But I am faceing a new issue with updating the records, its showing me NullPointerException in Service method save , guess i should ask a seprate question for that

Gaurav
  • 13
  • 6

0 Answers0