区别
- Model是每次请求中都存在的默认参数,可以自动创建,利用其addAttribute()方法即可将服务器的值传递到jsp页面中
- ModelAndView,使用时需要自己实例化(new)
- Model只是用来传输数据的,并不会进行业务的寻址。ModelAndView 却是可以进行业务寻址的
ModelAndView的使用
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class AnnotationController {
@RequestMapping("/ahello")
public ModelAndView hello() {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","hello mvc");
mv.setViewName("hello");
return mv;
}
}
Model的使用
package demo01.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AnnotationController {
@RequestMapping("/ahello")
public String hello(Model m) {
m.addAttribute("amsg","annotation mvc");
return "hello";
}
}