Model和ModelAndView的使用

Model:

    public String index(Model model){
        model.addAttribute("msg","你好,世界!");
        List list = new ArrayList<>();
        list.add("学习");
        list.add("在学习");
        list.add("在学习吗?");
        list.add("真的在学习吗?");
        model.addAttribute("list",list);
        model.addAttribute("user",new User("admin","123456",18));
        return "index";
    }

ModelAndView:

    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","你好,世界!");
        mv.addObject("msg2","你好,世界!");
        List list = new ArrayList<>();
        list.add("学习");
        list.add("在学习");
        list.add("在学习吗?");
        list.add("真的在学习吗?");
        mv.addObject("list",list);
        mv.addObject("user",new User("admin","123456",18));
        mv.setViewName("index");
        return mv;
    }

你可能感兴趣的:(SpringMVC)