0

In a Spring controller, I am performing two iterations of an Entity as shown below : Students and Course needed to be passed to the view

//this is the first iterations
List<Students> searchList = studentService.getAllStudents(splitStr[0]);

    for(Students listItems : searchList){

        String firstname = listItems.getFirstname();
        String lastname = listItems.getLastname(); 

    }
//this is the second iterations
List<Course> courseList = courseService.getAllCourse(splitStr[0]);

        for(Course listItems : searchList2){

            ... 

        }

I can only pass one of the list to the view at the moment as shown here

return new ModelAndView("searchList", "searchList", searchList);

My challenge is to pass this list to the view as well, but I can only return one ModelAndView object

return new ModelAndView("searchList", "searchList", courseList);
Blaze
  • 2,269
  • 11
  • 40
  • 82

3 Answers3

2

Use map:

Map<String,Object> model = new HashMap<String,Object();
model.put("courseList", courseList);
model.put("searchList", searchList);

new ModelAndView("theView", model)
HeyHo
  • 49
  • 4
2

As presented here, you can do it like this:

ModelAndView mapCourseList = new ModelAndView("searchList");
mapCourseList.addObject("searchList", courseList);

return mapCourseList;
Kh.Taheri
  • 946
  • 1
  • 10
  • 25
0

You can also do by using below code:

@RequestMapping("/list")
public String getLists(Model model){
    model.addAttribute("courseList", courseList);
    model.addAttribute("searchList", searchList);
    return "viewName";
}