将两个实体(学生,课程)迭代的列表传递给弹簧视图

时间:2017-09-22 09:51:29

标签: java spring

在Spring控制器中,我正在执行两个实体迭代,如下所示:学生课程需要传递给视图

//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){

            ... 

        }

我现在只能将其中一个列表传递给视图,如此处所示

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

我的挑战是将此列表传递给视图,但我只能返回一个ModelAndView对象

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

3 个答案:

答案 0 :(得分:2)

使用地图:

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

new ModelAndView("theView", model)

答案 1 :(得分:2)

如上所述here,你可以这样做:

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

return mapCourseList;

答案 2 :(得分:0)

您也可以使用以下代码:

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