Model model,HttpServletRequest request, ModelMap map声明变量
request.getSession().setAttribute("test", "1");
request.setAttribute("test", "2");
map.addAttribute("test", "3");
model.addAttribute("test", "4");
request.getSession().setAttribute("test", "1");
request.setAttribute("test", "2");
map.addAttribute("test", "3");
model.addAttribute("test", "4");
jsp页面通过${test}或者request.getAttribute("test")这个方式取值,优先取Model或ModelMap,Model和ModelMap谁最后赋值就取谁,然后是request,最后是session
使用spring参数绑定对象时,会在controller被调用之前在ModelMap里面放置参数对象对应的数据,例如:
@RequestMapping("/test")
public String test(TestBean test,HttpRequest request,ModelMap map){
request.setAttribute("testBean ", "2"); //jsp中如果有${testBean }只会取到test,而不会取到2
return "view";
}
进入这个方法的时候,map中就有一个key为testBean的对象,这个对象正是test。